Forum Moderators: martinibuster

Message Too Old, No Replies

Stop Adsense displaying to certain IP addresses

Is it possible?

         

bouncybunny

4:59 pm on Mar 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just wondering if it is possible to stop Adsense ads appearing to visitors from certain IP addresses?

I would like to be able to block a whole range of IPs if possble.

.htaccess maybe?

Philosopher

5:08 pm on Mar 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sure...place the adsense in a file all it's own.

Then include that file via php/asp etc. using some code to check against a list of IPs. IF the IP is found, don't show, if not found show.

bouncybunny

5:24 pm on Mar 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks.

Problem is I already have several hundred individual adsense ads around my site on static pages. I was hoping for a serverwide solution somehow, without having to recode every page on my site.

It's Linux/Apache server.

Even better would be to block by country domain, e.g. .co.uk or whatever.

jatar_k

5:29 pm on Mar 30, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



well you could all out ban certain ips with htaccess

if that's what you want, not sure you could just not show ads using it

as Philosopher said, you could easily control the ads using php

you could have the html extension parsed by php, this would allow you to add code but not have to change file extensions. Just a thought, changing technologies is not something I recommend, in this case adding a technology may be an advantage, you would have to look at the cost of implementing it.

bouncybunny

5:34 pm on Mar 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I wouldn't want to block access to the whole web site.

The pages are static, but have .php extensions as the site uses some php includes.

Having said that I am pretty far from being a coder and I was hoping for a simple copy and paste solution ideally.

jatar_k

5:37 pm on Mar 30, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



fair enough

do you have a site wide include that you use? if so maybe you could use that to put a little code in.

is all of your adsense code pasted into the actual page? or is it in an included file?

bouncybunny

5:41 pm on Mar 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



All pasted into individual pages unfortunately. I never realised that this site would get so big when I started it. ;)

jatar_k

5:48 pm on Mar 30, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



well you could have a little snippet that would do it but you would have to paste it around your adsense code on every page

something like this would work

<?php
$visitorIP = $_SERVER['REMOTE_ADDR'];
$blockedips = array('123.45.67.89', '123.56.67.89', '123.67.67.89');
if (!in_array($visitorIP,$blockedips)) {
?>
// adsense code here
<?php
}
?>

you paste your adsense code where I marked and then you add the ips you want to block to that array. You would better to store the array in an included file so then at least that could be edited in a sungle location.

remember if you block an ip from someone like AOL then you probably block all of AOL.

bouncybunny

6:04 pm on Mar 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks a lot jatar_k Much appreciated.

That might be especially useful in the dynamic parts of my site (.e.g. the forum).

I should at some point get around to using some include files for the static pages. :-/

Would it be possible to do this to block a range of IPs, using wildcards. e.g. 192.168.*.* or 192.168.?

Would this work?

<?php
$visitorIP = $_SERVER['REMOTE_ADDR'];
$blockedips = array('123.45.', '123.56.', '123.67.67.');
if (!in_array($visitorIP,$blockedips)) {
?>
// adsense code here
<?php
}
?>

I'm also assuming this doesn't violate any Adsense TOS?

jatar_k

6:19 pm on Mar 30, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



no the ranges wouldn't work because that code is simple and looks for the whole string, as opposed to looking at each part. You could make a lot more complex code than what I have there, that is about as simple as it can be.

and no this wouldn't violate TOS, you are not touching the code, you are just deciding who to serve it to. TThis would be the same as a site that doesn't show ads to logged in users.

bouncybunny

6:38 pm on Mar 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK thanks.