Jump to content

How do I hide my Google Analytics code?


baldbrian

Recommended Posts

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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";
}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.