[ Google-related ]
myIP() Cross Your Fingers Click Happens Google Gadgets SE Spam Google Data Centers

myIP()

use php to set the value of the current IP address in a Javascript variable

[ My IP address is 38.103.63.17 ]

Circa 2006 there was no native method available in Javascript to obtain the IP address of a visitor to your site. There are many cases where this would be useful but for me I mostly wanted to avoid jeopardizing my AdSense account by accidentally clicking on one of my own ads. I wanted to be able to simply insert one line in the script just before the definition of the google_ad_client that would set google_adtest=on if the page request came from my IP. However, it is more correct to place it in a separate block of code above the ad (thanks Jenstar!)

This example assumes myip.php is in the current directory but it can be located anywhere as long as it can reached by the URL given in the src="..." attribute of the script tag.

This code is required once per page:
<script type="text/javascript" src="myip.php">
var google_adtest='';
</script>

This code should be placed above each ad:
<script type="text/javascript"><!--
if( myIP() ) google_adtest='on';
//--></script>
<script type="text/javascript"><!--
google_ad_client = "pub-0000000000000000";
google_ad_width = 468;
google_ad_height = 60;
...

The following technique works because the webserver will interpret the file as a php script by virtue of its extension being .php yet the browser will interpret any output from the php script as Javascript source because it has been identified as such with the <script>'s tag attribute src.

myip.php

php generated Javascript
  var arMask = new Array();
// set your IP address(es) in the array arMask
  arMask[0] = new Array('127','220','63','*');   // home
  arMask[1] = new Array('127','44','222','130');  // office
  arMask[2] = new Array('127','51','56','*');   // wireless 

// optional border around ads
  var bShowBorder=true;

<?php 
$ip 
$_SERVER['REMOTE_ADDR']; 
echo 
"var theIP = '$ip';\n"
?>

  var bMine = 2; // 2=maybe
  function myIP(given)
  {
    var rtn = bMine;
    if( given!=null )
    { arMask = given.split(',');
      rtn = 2;
    }
    if( rtn==2 ) 
    {
      var arIP = theIP.split('.');
      var cnt = arMask.length;
      var i,j;
      rtn = 0;
      for( i=0 ; i<cnt ; i++ )
      { for( j=0 ; j<4 ; j++ )
        { if( arMask[i][j]!='*' && arMask[i][j]!=arIP[j] ) break;
        }
        if( j==4 )
        { rtn=1;
          break;
        }
        if( rtn ) break;
      }
      bMine = rtn;
    }
    if( rtn && bShowBorder )
    {
v = '<div';
v = v + ' style="';
v = v + ' padding:2px;';
v = v + ' background-color: #ffff66;';
v = v + ' border:dashed 1px #ff6666;';
v = v + '">';
      document.write(v);
    }

    return rtn;
  }

This example is based on editing the file with your IP addresses but you also have the option of passing a single address or even a comma-delimited list of addresses as an argument to the myIP function like this:
 myIP('127.0.0.*,10.1.1.1'); 

So - to pull it all together and implement myIP() on your site there are just a few things to be done:

  1. Put a copy of myip.php on your site.
  2. Edit your copy of myip.php so it contains your own IP address. (Looks like it should at least be set up for 38.103.63.17 in your case.)
  3. Add the blocks of code as shown above to the pages containing AdSense scripts.

Once you have installed mypip.php, set it up correctly with your own IP address(es) and you have the left the variable bShowBorder set to its default value of 'true' then you should see a distinctive yellow border around your ads something like the sample on the left whenever you are looking at your own pages from one of your own IP's

... so if you see the border you can relax because if you accidentally click on one of your own ads now, it's no big deal.


Note: There is a deprecated AJAX-based version of this technique available for reference purposes.

Google Pack contains 13 of the world's best programs. Install just one of them or all of them for free





DISCLAIMER: The information on this site is provided as is. The author(s) make no claim as to its correctness or fitness for any purpose. This website is not affiliated with or approved by Google. In fact every time they see it, Larry Page and Sergey Brin just shake their heads in kind of a sad way and wonder what this world is coming to.


© 2008 Michael Thompson