Forum Moderators: martinibuster
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] 2. Include the adsense.php file, and write the adsense code snippet if the ip is okIn 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]
Deny From #*$!.xxx.xxx.xxx in your .htaccess?
If you want to block by range, just omit last three octaves
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.
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)
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 ModificationAny 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.
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.
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 ;)
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.)
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
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.
[edited by: jatar_k at 7:38 pm (utc) on June 7, 2006]
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?
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 :)