1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP code not to click/display your own Google Ads

Discussion in 'AdSense' started by lindavdz, Mar 18, 2006.

  1. #1
    I know I've read it in one of the posts before, but the "Search" feature didn't really provide the topics for me.

    Even though I use a Firefox plugin to block Ads, I'm worried that one sweet day I'll have adblock turned off and I'll accidentally click my ads.
    It's not very likely, but it can happen.
    SEMrush
    Someone once posted a piece of PHP code to put on the website so the ads will not be displayed to a certain IP address. Hence, you can't click your own ads if you can't see them. :)

    Can anyone provide this code for me? Or shine a light on this subject?

    Thanks.
     
    lindavdz, Mar 18, 2006 IP
    SEMrush
  2. Greg-J

    Greg-J I humbly return to you.

    Messages:
    1,844
    Likes Received:
    153
    Best Answers:
    0
    Trophy Points:
    135
    #2
    I haven't seen any on the forums, but it's not a difficult one to do:

    First get the ip of the user accessing the site:
    
    $myip = $_SERVER['REMOTE_ADDR'];
    
    Code (markup):
    Now find the IP addresses of all the computers that you would visit your site/adsense account and assign them variables.

    
    $home = "68.186.1.197";
    $work = "32.142.23.2";
    $school = "24.77.132.92";
    
    Code (markup):
    Define your adsense code:
    
    $adcode = <<<HTML
    <script type="text/javascript"><!--
    google_ad_client = "pub-xxxxxxxxxxxxx";
    google_ad_width = 160;
    google_ad_height = 600;
    google_ad_format = "160x600_as";
    google_ad_type = "text_image";
    google_ad_channel ="";
    google_color_border = "333333";
    google_color_bg = "574D2E";
    google_color_link = "FFFFFF";
    google_color_url = "574D2E";
    google_color_text = "CCCCCC";
    //--></script>
    <script type="text/javascript"
      src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
    </script>
    HTML;
    
    Code (markup):
    Do a little comparing:
    
    if($myip !== $home && $myip !== $work && $myip !== $school)
    echo $adcode;
    
    Code (markup):
    So in the end you should have something like this:
    
    [B][COLOR="Red"]<?php[/COLOR][/B]
    $myip [COLOR="Blue"]=[/COLOR] [COLOR="MediumTurquoise"]$_SERVER[/COLOR][[COLOR="Red"]'REMOTE_ADDR'[/COLOR]];
    $home [COLOR="Blue"]=[/COLOR] [COLOR="Red"]"68.186.1.197"[/COLOR];
    $work [COLOR="Blue"]=[/COLOR] [COLOR="Red"]"32.142.23.2"[/COLOR];
    $school [COLOR="Blue"]=[/COLOR] [COLOR="Red"]"24.77.132.92"[/COLOR];
    $adcode [COLOR="Blue"]= <<<[/COLOR]HTML
    [COLOR="DarkRed"]<script type="text/javascript">[/COLOR][COLOR="Silver"]<!--[/COLOR]
    google_ad_client = "pub-xxxxxxxxxxxxx";
    google_ad_width = 160;
    google_ad_height = 600;
    google_ad_format = "160x600_as";
    google_ad_type = "text_image";
    google_ad_channel ="";
    google_color_border = "333333";
    google_color_bg = "574D2E";
    google_color_link = "FFFFFF";
    google_color_url = "574D2E";
    google_color_text = "CCCCCC";
    [COLOR="Silver"]//-->[/COLOR][COLOR="DarkRed"]</script>[/COLOR]
    [COLOR="DarkRed"]<script type="text/javascript"[/COLOR]
      src="http://pagead2.googlesyndication.com/pagead/show_ads.js"[COLOR="DarkRed"]>[/COLOR]
    [COLOR="DarkRed"]</script>[/COLOR]
    HTML;
    [COLOR="SeaGreen"]if[/COLOR]($myip [COLOR="Blue"]!==[/COLOR] $home [COLOR="Blue"]&&[/COLOR] $myip [COLOR="Blue"]!==[/COLOR] $work [COLOR="Blue"]&&[/COLOR] $myip [COLOR="Blue"]!==[/COLOR] $school)
    [COLOR="SeaGreen"]echo[/COLOR] $adcode;
    [B][COLOR="Red"]?>[/COLOR][/B]
    
    Code (markup):
    I haven't tested this code, but it should work. If not, let me know and I'll fix whatever syntax error I've created. You're welcome to use it, modify it, whatever - I don't care.
     
    Greg-J, Mar 18, 2006 IP
  3. lindavdz

    lindavdz Peon

    Messages:
    264
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I'll start testing and let you know. Thanks a lot!
     
    lindavdz, Mar 19, 2006 IP
  4. TommyD

    TommyD Peon

    Messages:
    1,397
    Likes Received:
    76
    Best Answers:
    0
    Trophy Points:
    0
    #4
    lindavdz,

    I was in your shoes, but then I realized coding in php has its own problems. What if you want to see the ad placement, you have to change the code, and upload it. Or what if you ip address changes?

    So, I settled with just making an entry in my 'hosts' file, and having a short cut on my desktop, so I can comment the line out, and in, as needed.

    0.0.0.0 pagead2.googlesyndication.com

    hth,

    tom
     
    TommyD, Mar 19, 2006 IP
  5. noppid

    noppid gunnin' for the quota

    Messages:
    4,246
    Likes Received:
    232
    Best Answers:
    0
    Trophy Points:
    135
    #5
    Just add a request variable to the the code for testing placement.

    www.example.com/index.php?show=1

    if(($myip !== $home && $myip !== $work && $myip !== $school) || intval($_REQUEST['show']) == 1)
     
    noppid, Mar 19, 2006 IP
  6. TommyD

    TommyD Peon

    Messages:
    1,397
    Likes Received:
    76
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Just a friendly tweak:

    if(($myip != $home && $myip != $work && $myip != $school) || $_REQUEST['show'] == 1)


    ;)
     
    TommyD, Mar 19, 2006 IP
    noppid likes this.
  7. noppid

    noppid gunnin' for the quota

    Messages:
    4,246
    Likes Received:
    232
    Best Answers:
    0
    Trophy Points:
    135
    #7

    Yeah, I caught that just after ya posted. Thanks.
     
    noppid, Mar 19, 2006 IP
  8. TommyD

    TommyD Peon

    Messages:
    1,397
    Likes Received:
    76
    Best Answers:
    0
    Trophy Points:
    0
    #8
    NoProb, should see what I type when I have my 4-year-old son jumping on me when I'm trying to do a quick post. Little Monkey.

    later,

    tom
     
    TommyD, Mar 19, 2006 IP
    Greg-J likes this.
  9. Greg-J

    Greg-J I humbly return to you.

    Messages:
    1,844
    Likes Received:
    153
    Best Answers:
    0
    Trophy Points:
    135
    #9
    Great suggestion Tommy.

     
    Greg-J, Mar 19, 2006 IP
  10. profs77

    profs77 Well-Known Member

    Messages:
    441
    Likes Received:
    39
    Best Answers:
    0
    Trophy Points:
    118
    #10
    How about making a special page that only you know about that sets a cookie for your domain. Then if the cookie exists, don't show your ads, else show them.
     
    profs77, Mar 19, 2006 IP
  11. Ratbert

    Ratbert Member

    Messages:
    18
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #11
    How about something little bit more reusable with Google recommended method using the adtest and ca-test:
    <?php
    function adsense($adformat, $adclient, $adchannel = '', $adtype = 'text_image', $cborder = 'FFFFFF', $cbg = 'FFFFFF', $clink = '0000FF', $curl = '008000', $ctext = '000000')
    {
    	$adtest = array('192.168.0.2', '192.168.1.200');
    
    	if (in_array($_SERVER['REMOTE_ADDR'], $adtest)) {
    		$client = 'google_adtest = "on";' . "\n" . 'google_ad_client = "ca-test"' . "\n";
    	} else {
    		$client = 'google_ad_client = "' . $adclient . '";' . "\n" . 'google_ad_channel = "' . $adchannel . '";' . "\n";
    	}
    
    	$x = preg_split('/x/', $adformat);
    	$y = preg_split('/_/', $x[1]);
    	$width = $x[0];
    	$height = $y[0];
    
    	$buffer='<script type="text/javascript"><!--' . "\n" .
    			$client .
    			'google_ad_width = ' . $width . ";\n" .
    			'google_ad_height = ' . $height . ";\n" .
    			'google_ad_format = "' . $adformat . '";' . "\n" .
    			'google_ad_type = "' . $adtype . '";' . "\n" .
    			'google_color_border = "' . $cborder . '";' . "\n" .
    			'google_color_bg = "' . $cbg . '";' . "\n" .
    			'google_color_link = "' . $clink . '";' . "\n" .
    			'google_color_url = "' . $curl . '";' . "\n" .
    			'google_color_text = "' . $ctext . '";' . "\n" .
    			'//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>';
    	return $buffer;
    }
    ?>
    PHP:
    To use with no channel, default colors and ad type:
    <?php echo adsense('728x90_as', 'pub-9876543210987654'); ?>
    PHP:
    Including channel, only text ad type and custom colors:
    <?php echo adsense('728x90_as', 'pub-9876543210987654', '9876543210', 'text', 'A8DDA0', 'EBFFED', '0000CC', '008000', '6F6F6F'); ?>
    PHP:
     
    Ratbert, Mar 19, 2006 IP
  12. Greg-J

    Greg-J I humbly return to you.

    Messages:
    1,844
    Likes Received:
    153
    Best Answers:
    0
    Trophy Points:
    135
    #12
    That's more overhead than you need in my opinion.

    I don't hide my ads from myself, as I don't see a point really. Just pay attention to your clicks and you'll be fine. If I were concerned though, I'd either use TommyD's suggestion using my hosts file - or I'd just set the visibility of my ads to hidden.

    In all honesty, it should be AdSense's responsibility to simply not collect/pay on ads clicked by IPs that have logged in to the AdSense account being clicked.
     
    Greg-J, Mar 19, 2006 IP
  13. yep

    yep Guest

    Messages:
    28
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Is it against the TOS for impressions based payment is you view your ads ?
     
    yep, Mar 20, 2006 IP
  14. Greg-J

    Greg-J I humbly return to you.

    Messages:
    1,844
    Likes Received:
    153
    Best Answers:
    0
    Trophy Points:
    135
    #14
    I'm not sure what you're asking yep. You could rephrase that?
     
    Greg-J, Mar 20, 2006 IP
  15. Mr. Fern

    Mr. Fern Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    No it is not @ yep.

    They just say avoid unnecessary page loads. Checking your site = fine, Reloading X amount of times = might stir some trouble.

    https://www.google.com/support/adsense/bin/answer.py?answer=18203
     
    Mr. Fern, Mar 20, 2006 IP
  16. TommyD

    TommyD Peon

    Messages:
    1,397
    Likes Received:
    76
    Best Answers:
    0
    Trophy Points:
    0
    #16
    G is weird at times.

    I've asked for ip address filtering, I would submit my own ip address, and they said NO.

    IMHO, I would think they were happy to find publishers trying to prevent invalid clicks.

    :confused:
     
    TommyD, Mar 20, 2006 IP
  17. john269

    john269 Notable Member

    Messages:
    6,229
    Likes Received:
    116
    Best Answers:
    0
    Trophy Points:
    235
    #17
    When I do testing on my site I normally take all the ads down and once my script works I then put the ads back and upload it and then see what the page looks like. If fine that's it, I'm done.
     
    john269, Mar 20, 2006 IP
  18. bpearson

    bpearson Peon

    Messages:
    173
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #18
    you could always show PSA's whne its your ip address so you can see the ad placements
     
    bpearson, Mar 20, 2006 IP
  19. john269

    john269 Notable Member

    Messages:
    6,229
    Likes Received:
    116
    Best Answers:
    0
    Trophy Points:
    235
    #19
    How can you manage to just show PSA's. I thought that Google only shows them when there is no ads available for your sites content.
     
    john269, Mar 20, 2006 IP
  20. Mr. Fern

    Mr. Fern Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #20
    I recall reading someone once saying they clicked on a PSA ad and got booted, thinking that clicking on a PSA would be nothing wrong. So even if it were possible to force PSAs (which I don't recall being possible), that doesn't help.
     
    Mr. Fern, Mar 21, 2006 IP