Blocking specific IPs from viewing your AdSense ads (or anything else you want)
Posted on 16/11/05 23:48
I just figured that blocking special IPs from viewing my ads would be a good idea (read: my own and my school's), that way there would be no way any invalid clicks would be registered from an IP I frequent together with 1200 other users and thus making the AdSense support team happy.
I set on to a journey to the support forums of my blog software, Nucleus CMS, they kindly provided me with this PHP code:
ads.php
The IPs blocked go into the array, contained by quotation marks and separated by commas, the AdSense code, or whatever you want to hide from a designated IP goes into the path on line 7, the path being the server path (such as /home/obli/obli.net/file.php in my case).
Ads.php is then included into wherever you want to (not) show your ads using:
...Or a special include tag unique to your blog software.
Props to Armon for writing the code, although I think it took him little effort, it's still a miracle to us who are not blessed with PHP.
I've gotta start learning some PHP...
Alternative way (does not work with adsense):
In case you want to ban IPs from viewing your entire website (or just a directory), that easy, just add this to your .htaccess file:
order allow,deny
deny from 12.34.56.78
allow from all
I set on to a journey to the support forums of my blog software, Nucleus CMS, they kindly provided me with this PHP code:
ads.php
<?php
$blocked_ips = array("12.34.56.78","98.76.54.32");
$current_ip = $_SERVER["REMOTE_ADDR"];
if (!in_array($current_ip, $blocked_ips)) {
include("path/to/adsense.txt");
}
?>
The IPs blocked go into the array, contained by quotation marks and separated by commas, the AdSense code, or whatever you want to hide from a designated IP goes into the path on line 7, the path being the server path (such as /home/obli/obli.net/file.php in my case).
Ads.php is then included into wherever you want to (not) show your ads using:
<?php include($DOCUMENT_ROOT . "ads.php");?>
...Or a special include tag unique to your blog software.
Props to Armon for writing the code, although I think it took him little effort, it's still a miracle to us who are not blessed with PHP.
I've gotta start learning some PHP...
Alternative way (does not work with adsense):
In case you want to ban IPs from viewing your entire website (or just a directory), that easy, just add this to your .htaccess file:
order allow,deny
deny from 12.34.56.78
allow from all




Ozh wrote: