Forum Moderators: coopster

Message Too Old, No Replies

PHP IP Ban

Banning by IP

         

redmanteam

4:21 am on Aug 6, 2008 (gmt 0)

10+ Year Member



I have quite a large list of IP addresses, each IP address is on it's own line in a text file (ban.txt)

eg

xx.180.206.184 #ban1
xx.26.7.10[01267] #ban2
xx.16.246. #ban3

Is there a snippet of code that I can add to the top of my php script. The code would compare the ip address of the incoming hit with the ip addresses in ban.txt

If there is a match, a message shows up "You are banned"

If there is no match, then the code below the snippet of ban code would execute.

[edited by: eelixduppy at 12:39 pm (utc) on Aug. 6, 2008]
[edit reason] obfuscated IPs [/edit]

Frank_Rizzo

8:22 am on Aug 6, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Much easier and quicker to do in apache. This way they get a ban notice even if they look at non php pages.

If you want to know the apache .htaccess way I can post my method. Else I will give way for someone to post the php solution.

paulmadillo

1:17 pm on Aug 6, 2008 (gmt 0)

10+ Year Member



Probably worth noting that ip banning isn't 100% successful as people can work through proxies etc.

redmanteam

4:36 pm on Aug 6, 2008 (gmt 0)

10+ Year Member



Hi Frank,

Thanks for the offer, I do have an htaccess solution that works, but I need a php solution that can be added to the top of an existing php script that will reference a text file with the IP addresses.

ag_47

12:23 am on Aug 7, 2008 (gmt 0)

10+ Year Member



What if the ip address changes, and a regular user gets a banned ip?
Unless you have a good reason to believe the same people are always accessing with the same ip, is this a good method of banning?

redmanteam

4:09 am on Aug 7, 2008 (gmt 0)

10+ Year Member



I am pretty familiar with modem users and changing ip addresses, but I have an issue with some ip addresses that for sure need to be banned.

RobBroekhuis

2:32 pm on Aug 8, 2008 (gmt 0)

10+ Year Member



You mean something more complicated than

if (strpos(file_get_contents("ban.txt","\n{$_SERVER['REMOTE_ADDR']} ")!==false) {
echo "Scram."; exit;
}

All this requires is that your ban.txt file starts with a newline, and each IP address is followed by a space (I don't understand the [01267] part).
Rob

redmanteam

8:00 pm on Aug 8, 2008 (gmt 0)

10+ Year Member



Thank you Rob, I'll give this a try.

Regarding the items in brackets xx.26.7.10[01267] that is the shorthand method in which I receive the IPs. The other scripts that use the same database recognize this shorthand.

Do you think your code would as well?

xx.26.7.10[01267] would be the same as:
xx.26.7.100
xx.26.7.101
xx.26.7.102
xx.26.7.106
xx.26.7.107

ag_47

8:39 pm on Aug 8, 2008 (gmt 0)

10+ Year Member



Check out how regular expressions work with PHP, should help you out. In particular the ereg and preg function set/family. As far as the above example, reading a text file line by line is a very simple process, and 12.1.1.[110] is considered a single string.

timestocome

12:29 am on Aug 9, 2008 (gmt 0)

10+ Year Member



I wrote a plugin for WP that uses PHP to ban ips. You can download it from WordPress [wordpress.org...]

You would scrap all the user control page stuff and just put a hook in your webpages to call it.

It should give you enough of a code example that you can adapt it for your website.