baldbrian Posted November 4, 2007 Share Posted November 4, 2007 I want to be able to "hide" my Google Analytics code whenever I personally visit one of my pages... what I've been trying to do is make it so that when I type in a url like this: www.mysite.com/index.php?hide=yes The page would load without the Google Analytics (and my visit would deliberately not be tracked). How do I do this? I've tried variations of the following code but always get errors... <?php $test=isset($_GET['test']) ? $_GET['test'] : false; if $test == "yes"1 { echo "ON"; } else { echo "OFF"; } ?> Even this simple code above gives erroneous results... what am I missing? Thanks! Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted November 4, 2007 Share Posted November 4, 2007 Always use BOOLs for this type of situation. www.mysite.com/index.php?hide=1 <?php if ( isset($_GET['hide']) && is_numeric($_GET['hide']) ) { $hide = intval($_GET['hide']); } // strongest test possible, since it should now be an integer if ( $hide === 1 ) { echo "ON"; } else { echo "OFF"; } ?> PhREEEk Quote Link to comment Share on other sites More sharing options...
btherl Posted November 5, 2007 Share Posted November 5, 2007 PHP actually has a native bool type, unlike some other languages. Internally it's stored as a long int. <?php $hide = ($_GET['hide'] === 'yes') ? true : false; if ( $hide === true ) { echo "ON"; } else { echo "OFF"; } ?> Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted November 5, 2007 Share Posted November 5, 2007 Right. Well the point I was trying to make was to eliminate 'words' in GET URLs where there is only one of two possible states. I just find it easier to validate and sanitize the incoming variable, and it makes for shorter URLs. PhREEEk Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted November 5, 2007 Share Posted November 5, 2007 What are the erroneous results? As far as I can tell the code you pasted has parse errors: <?php $test=isset($_GET['test']) ? $_GET['test'] : false; if $test == "yes"1 { echo "ON"; } else { echo "OFF"; } ?> Where are the parens around the if condition? And why do you have an 1 following "yes" in the conditional? Always use BOOLs for this type of situation. www.mysite.com/index.php?hide=1 It really doesn't matter what value is used, whether it be 1, yes, or foobar. They're all strings on the incoming page so no matter what you're going to do a string comparison. You aren't saving any work by converting it to an int and it isn't any more secure. Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted November 5, 2007 Share Posted November 5, 2007 Always use BOOLs for this type of situation. www.mysite.com/index.php?hide=1 It really doesn't matter what value is used, whether it be 1, yes, or foobar. They're all strings on the incoming page so no matter what you're going to do a string comparison. You aren't saving any work by converting it to an int and it isn't any more secure. We may have been posting at the same time, so my post above yours explains my recommendation. I did not say it was more secure up front coming in, I was trying to point out that it is easier (to me.. no further argument really necessary) to validate and secure after it comes in. I always use a function to do the validation and conversion to an integer, so multiple incoming 1's or 0's can be quickly validated. It's just coding preference is all, and also as stated, makes for shorter URLs. PhREEEk Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.