Forum Moderators: coopster

Message Too Old, No Replies

Blocking a certain ip range via php

         

jake66

9:18 am on Jan 16, 2008 (gmt 0)

10+ Year Member



I am currently using this code to block people from "Los Angeles" & "Beverly Hills" from a section of my site (for example).

$host = strtolower($_SERVER['REMOTE_HOST']);
$ip = $_SERVER['REMOTE_ADDR'];

if (strpos($host,'angeles') ¦¦ strpos($host,'beverly') && $ip!='123.456.78.9'){
header('Location:http://www.example.com/losangeles.html');
}

(The $ip portion to allow a moderator to edit content that happens to be from that part of the world)

I also want to ban people from the 12.345. IP range from that section too. I tried this:

if (strpos($host,'angeles') ¦¦ strpos($host,'beverly') ¦¦ strpos($ip,'12.345') && $ip!='123.456.78.9'){
header('Location:http://www.example.com/losangeles.html');
}

...but it's not working. Even if I place a full IP address there, it doesn't block. Is this impossible?

[edited by: jake66 at 10:00 am (utc) on Jan. 16, 2008]

[edited by: dreamcatcher at 9:31 am (utc) on Jan. 17, 2008]
[edit reason] Use example.com, thanks. [/edit]

venelin13

9:32 am on Jan 16, 2008 (gmt 0)

10+ Year Member



You miss a couple of )

Try this:

if ((strpos($host,'angeles') ¦¦ (strpos($host,'beverly') ¦¦ (strpos($ip,'123.456.') && ($ip!='12.345.78.9')){
header('Location:http://www.example.com/losangeles.html');
}

[edited by: dreamcatcher at 9:31 am (utc) on Jan. 17, 2008]
[edit reason] Use example.com, thanks. [/edit]

jake66

9:52 am on Jan 16, 2008 (gmt 0)

10+ Year Member



I actually tried to edit those, but my internet went down. :)

jake66

9:53 am on Jan 16, 2008 (gmt 0)

10+ Year Member



Tried adding the extra () for the if prefix and suffix, still no luck.

I am on php5 if it makes any difference.

lammert

2:20 pm on Jan 16, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I think it is in the details, you might have to put a space after Location: and before the http://

phparion

4:05 pm on Jan 16, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Have you heard about (geoIP) free database? once you install that you need only three lines, that are shown int he demo examples of this database website, to block any country IP series.

You can do the same with Apache Mod geoIP.

jake66

3:54 am on Jan 17, 2008 (gmt 0)

10+ Year Member



I tried adding a space between each side of Location, still no luck.

Also tried this:

if (strpos($ip,'11.22.33')){
header(' Location: http://www.example.com');
}

(tested my own IP, and it didn't work either. I got my IP directly off $_SERVER['REMOTE_ADDR'] )

Any reason that isn't even checking the IP, which seems to be why the redirect isn't working.

Have you heard about (geoIP) free database?

Yes, I initially tried that, but I don't need to sniff the user location as I already know the IP range and cities I need to filter. I don't really want to connect to a remote database either.. just about every free one had a remotely hosted database for you to use. Since I'm using this on a relatively large site I'd rather do everything in-house.

[edited by: dreamcatcher at 9:31 am (utc) on Jan. 17, 2008]
[edit reason] Use example.com, thanks. [/edit]