homepage Welcome to WebmasterWorld Guest from 54.167.11.16
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Google / Google AdSense
Forum Library, Charter, Moderators: incrediBILL & jatar k & martinibuster

Google AdSense Forum

    
Blocking Adsense by IP address ranges
and prevent clicking on your own ads (php script)
dhiggerdhigger




msg:1433356
 2:20 pm on May 18, 2006 (gmt 0)

I've been looking around for a this blocking-by-ip-range script for a while, including on WW, and it's been hard to come across. Lots of folks say "oh, I just do such-and-such in php". This is helpful, but those of us whose web programming skills consist of only intelligent cut-and-pasting followed by a tweak here and there, I include the full instructions for this task below!

I wanted to block ads not only from myself, but from my University too. This is because the content of my site is technical info about widgets, and and I direct my students on a Widgets lecture course to the site for more information. As a result, there is likely to be a concentration of clicks from one IP range - or even from a single IP for those browsing through a proxy. I don't want to risk being banned for this. Also, not serving ads to yourself protects your Click Through Rate (CTR), if you're a frequent visitor to your own site.

1. Save this code as adsense.php

[pre]<?php
// tells getip() if one of the ips it detects is valid or not.
function validip($ip)
{
if (!empty($ip) && ip2long($ip)!=-1)
{// reserved IANA IPv4 addresses
// hxxp://www.iana.org/assignments/ipv4-address-space
$reserved_ips = array (
array('0.0.0.0','2.255.255.255'), array('10.0.0.0','10.255.255.255'),
array('127.0.0.0','127.255.255.255'), array('169.254.0.0','169.254.255.255'),
array('172.16.0.0','172.31.255.255'), array('192.0.2.0','192.0.2.255'),
array('192.168.0.0','192.168.255.255'), array('255.255.255.0','255.255.255.255'));
foreach ($reserved_ips as $r)
{ $min = ip2long($r[ 0 ]); $max = ip2long($r[ 1 ]);
if ((ip2long($ip) >= $min) && (ip2long($ip) <= $max)) return false;
}
return true;
}
else return false;
}

// detects the user's ip

function getip()
{
global $HTTP_SERVER_VARS;
if (isset($HTTP_SERVER_VARS['HTTP_CLIENT_IP']) && validip($HTTP_SERVER_VARS['HTTP_CLIENT_IP']))
return $HTTP_SERVER_VARS['HTTP_CLIENT_IP'];
elseif (isset($HTTP_SERVER_VARS['HTTP_X_FORWARDED_FOR']) && $HTTP_SERVER_VARS['HTTP_X_FORWARDED_FOR']!="")
{
$forwarded=str_replace(",","",$HTTP_SERVER_VARS['HTTP_X_FORWARDED_FOR']);
$forwarded_array=split(" ",$forwarded);
foreach($forwarded_array as $value) if (validip($value)) return $value;
}
return $HTTP_SERVER_VARS['REMOTE_ADDR'];
}

function ShowAdsense()
{
// return 1; // uncomment this in order to see ads no matter what ip detects.

$detectedip = getip();

// repeat this block (next two lines), only changing $testip if you have several ip's
$testip="129.11."; // <-- works on ip ranges, but can specify whole IP if you like
if (substr($detectedip,0,strlen($testip))==$testip) return 0;

return 1;
}

?>[/pre]

Notice my ip ranges at the bottom: 163.160.x.x and 129.11.x.x. You can change these to whatever you like. Just repeat the $testip=... line and the line following for other ip addresses or ip ranges.

2. Include the adsense.php file, and write the adsense code snippet if the ip is ok

In any of your php-parsed web pages, insert this code:
[pre]<?php
include("/path/on/your/server/to/adsense.php"); // test the ip
if (showAdsense()==1) { // show the ad if ip is ok
echo "<script type=\"text/javascript\"><!--\n"
." google_ad_client = \"pub-0000000000000000\";\n"
." google_alternate_ad_url = \"http://www.example.com/your_ad_alternate.html\";\n"
." google_ad_width = 468;\n"
." google_ad_height = 60;\n"
." google_ad_format = \"468x60_as\";\n"
." google_ad_type = \"text\";\n"
." google_ad_channel =\"0000000000\";\n"
." google_color_border = \"FFFFFF\";\n"
." google_color_bg = \"FFFFFF\";\n"
." google_color_link = \"0046D6\";\n"
." google_color_url = \"9D6767\";\n"
." google_color_text = \"181818\";\n"
." //--></script>\n"
." <script type=\"text/javascript\"\n"
." src=\"http://pagead2.googlesyndication.com/pagead/show_ads.js\">\n"
." </script>";
}
?>[/pre]

You'll have to alter this code so that your adsense code is exactly reproduced as Google expects it. Also make sure that the path to the adsense.php file is the correct path on your server. Note that none of this process is seen by the browser - or by Google's bots. Either the adsense code is produced in the source of a web page, unaltered, or it is completely absent.

Note also that to allow ads temporarily to all IPs, you just have to uncomment one line in the ShowAdsense() function.

Hope this is helpful.

[edited by: jatar_k at 5:09 pm (utc) on May 18, 2006]
[edit reason] fixed sidescroll [/edit]

 

toldan




msg:1433357
 6:35 pm on May 18, 2006 (gmt 0)

Can't you use:

Deny From #*$!.xxx.xxx.xxx in your .htaccess?

If you want to block by range, just omit last three octaves

toldan




msg:1433358
 6:35 pm on May 18, 2006 (gmt 0)

Deny From 123.123.123.123

ZenArcher




msg:1433359
 6:46 pm on May 18, 2006 (gmt 0)

Very interesting! Thanks!

Some helpful/picky comments: the traditional programming way to return a default result is "0". If we agree that showing the ads is the "default result" then the adsense.php code could be reversed to return 0 when showing ads, and 1 or -1 when a blocked IP is found.

The code snippet would then read:
"if (showAdsense())" -- which would be true when equal to 0, and any non-zero result evaluates to "false".

Also, if the echo'ed AdSense code is delimited with single quotes, then the backslashes are not needed for double quotes in the javascript equations.

Rodney




msg:1433360
 6:59 pm on May 18, 2006 (gmt 0)

You'll have to alter this code so that your adsense code is exactly reproduced as Google expects it.

I know people probably do it all the time, but I think, according to the Adsense Terms and Conditions, you are not allowed to modify the adsense code like the manner you have shown above (by escaping the quote marks, etc)

hdpt00




msg:1433361
 7:09 pm on May 18, 2006 (gmt 0)

Rodney, that is just PHP escaping the quotes so it runs. No TOS violations.

Rodney




msg:1433362
 7:30 pm on May 18, 2006 (gmt 0)

Rodney, that is just PHP escaping the quotes so it runs. No TOS violations.

I know what it is :), but are you certain it's not a TOS violation?

I'm pretty sure that it was commented here by AdsenseAdvisor that modifying the code was against the TOS and to make sure that any script that you use doesn't modify the adsense code.

From this page:
[google.com...]

Code Modification

Any AdSense ad code, search box code, or referral code must be pasted directly into Web pages without modification. AdSense participants are not allowed to alter any portion of the ad code or change the layout, behavior, targeting, or delivery of ads for any reasonb.

I put those parts in bold just to show why I even brought it up.

Like I said, I know a lot of people probably do it all the time and I know it's used to escape quotes in PHP, but I wanted to bring it up just in case people didn't know that it might be a TOS violation. You can always check with Google directly (which is always a good idea), but that's my understanding of it.

BigDave




msg:1433363
 8:31 pm on May 18, 2006 (gmt 0)

ZenArcher, 0 is false, and any other value is true. Think of it this way, Zero is "nothing" and any other value represents "something". Then try reading the statement "if nothing" and "if something".

Rodney, the source code is what you are looking at, the HTML of the web page is what is produced. The final result produced by the code is the webpage and it comes out EXACTLY as google gives it to you, so there is no violation of the TOS.

jatar_k




msg:1433364
 8:39 pm on May 18, 2006 (gmt 0)

another point is that if you just use single quotes around those strings then you don't even need to escape them

you also don't need to concatenate all the parts when you are using echo. Just seperate the parts with a comma.

you need to leave \n double quoted or it won't actually be interpretted as a newline

something like this

<?php 
include("/path/on/your/server/to/adsense.php"); // test the ip
if (showAdsense()==1) { // show the ad if ip is ok
echo '<script type="text/javascript"><!--',"\n"
,' google_ad_client = "pub-0000000000000000";',"\n"
,' google_alternate_ad_url = "http://www.example.com/your_ad_alternate.html";',"\n"
,' google_ad_width = 468;',"\n"
,' google_ad_height = 60;',"\n"
,' google_ad_format = "468x60_as";',"\n"
,' google_ad_type = "text";',"\n"
,' google_ad_channel ="0000000000";',"\n"
,' google_color_border = "FFFFFF";',"\n"
,' google_color_bg = "FFFFFF";',"\n"
,' google_color_link = "0046D6";',"\n"
,' google_color_url = "9D6767";',"\n"
,' google_color_text = "181818";',"\n"
,' //--></script>',"\n"
,' <script type="text/javascript"',"\n"
,' src="http://pagead2.googlesyndication.com/pagead/show_ads.js">',"\n"
,' </script>';
}
?>

I think I got all the pairs right ;)

gregbo




msg:1433365
 8:45 pm on May 18, 2006 (gmt 0)

BTW, the X-Forwarded-For field can contain RFC 1918 (private) addresses. So if for some reason the source IP was not set, but you got a private X-Forwarded-For IP, it might be a legit click.

To whoever uses such scripts, make sure you keep in touch with the people who administer your networks, so if they change the network addresses you can make the necessary script changes. (Even better if you can get them to post their changes in a public place so they can be automatically fetched and incorporated in your script.)

dhiggerdhigger




msg:1433366
 8:51 pm on May 18, 2006 (gmt 0)

Thanks everyone for your comments.

One other thing which may make the code more useful would be to put the second code snippet (the one with the adsense code in it) into it's own include file, and then include THAT into each web page. That way, if you change the ad, you won't have to change the code on every page, just the code in the included file.

<?php include("/your/server/path/to/my_ad.php");?>

Dave

hdpt00




msg:1433367
 8:58 pm on May 18, 2006 (gmt 0)

Rodney if you know what it is, then you obviously don't know how PHP interprates it, the code is fine. Why don't you paste that code on your page iwthin a php echo and view source and see what it looks like.

Rodney




msg:1433368
 9:23 pm on May 18, 2006 (gmt 0)

Rodney, the source code is what you are looking at, the HTML of the web page is what is produced. The final result produced by the code is the webpage and it comes out EXACTLY as google gives it to you, so there is no violation of the TOS.

I know the end result is exactly what Google gives you, but you have to modify the code (by escaping the quotes) in order to make it do that.

The policy that I quoted said that you cannot modify the code for any reason (and by any, I'm just assuming that escaping the code so that it works with php falls under that).

So even though when anybody acually loads the webpage, the code is intact and it comes out right in the end, you still have to modify the code, which is against the TOS as I read it and as I understood from AdsenseAdvisor's comments.

That's just my opinion from reading the TOS and reading ASA's comments about other scripts, feel free to come to your own conclusions.

Why don't you paste that code on your page iwthin a php echo and view source and see what it looks like.

Because I don't want to be in violation of the TOS as I understand it :)

I know what the code will produce as the end result, but I have to modify the adsense code to get it to do that.

hdpt00




msg:1433369
 10:02 pm on May 18, 2006 (gmt 0)

On a final note, you aren't modifying the code at all, all Google bots will see is what you see on view source, the EXACT same thing as if you didn't escape in PHP, so get over it buddy.

[edited by: jatar_k at 7:38 pm (utc) on June 7, 2006]

Rodney




msg:1433370
 10:13 pm on May 18, 2006 (gmt 0)

Rodney, you are being very ignorant.

On a final note, you aren't modifying the code at all, all Google bots will see is what you see on view source, the EXACT same thing as if you didn't escape in PHP, so get over it buddy.

I'm not sure why you are getting upset and making personal attacks?

I'm not telling anybody what to do with their site, I'm just pointing out my opinion and how I understand the TOS. I'm fullly aware I could be wrong, but there is the possibility that I could be right based on what AdsenseAdvisor has stated here.

If you can't understand that you ARE modifying the code, then that's your own issue.

Just because the Googlebot sees the correct code, doesn't mean that you didn't MODIFY the code in the php script to get it to format perfectly for the browser.

If the code originally says this:
google_ad_client = "pub-0000000000000000"

and you have to edit it to say this:
google_ad_client = \"pub-0000000000000000\";\n"

in your php code, then that is a modification. This is probably just semantics, but I consider it a modification. What if someone mistypes an extra quote or an escape?

But like I've said before in other threads, what I say doesn't matter a bit. What matters is what Google thinks about it. I haven't heard anything from Google that says that the policy quoted above doesn't apply to PHP code which gets parsed correctly by the browser or is seen as unmodified by Googlebot.

As I've said several times in this thread, many people are doing it and probably have been doing it for a long time without issue. But there is the possibility that it is against the TOS.

People are free to do what they want. No need to get upset or resort to namecalling, I just wanted to point out my interpretation. What could it hurt to get the official word from Google?

jatar_k




msg:1433371
 12:58 am on May 19, 2006 (gmt 0)

>> People are free to do what they want.

the literal interpretation is correct, adding those slashes does change the code, even though adsense would never know as it would be exactly the same in the browser

my changes to the code make it a moot point anyway ;)

so we can all see both sides, we're all friends here, no need to belabour the point

enough said :)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Google / Google AdSense
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved