PHP file read to ban IP addresses?

bo75007

New member
<?php
$url = 'put your ip banlist filename here';
$iplist = explode(',', file($url));

for ($j=0; $j < count($iplist); $j++)
{
if(preg_match('#' . $iplist[$j] . '#i', $_SERVER['REMOTE_ADDR']))
{
// This is where you put what you want to happen
// For now we will just kill the script because I am lazy, you can change it to a redirect later
die('You are banned.');
}
}
?>


Sorry I didnt do any debugging or syntax checking and Yahoo Answers removes extra whitespace so bad formatting. Hopefully I didn't make any mistakes, try it out ;)
 
Hey guys.

Basically I have a text file of comma separated IP addresses (e.g. 12.34.567.89,876,98,987,65 etc) and I need some PHP code to see if the client's IP address is on the ban file. If the user's IP is on the banned list (the comma separated file) they must be directed to example.com/banned (I will change that later).

This code is for a chat room I am building. I already have existing fwrite code to add the IPs, but not to check them when the offender tries to reaccess my chat!

Thanks
 
<?php
$url = 'put your ip banlist filename here';
$iplist = explode(',', file($url));

for ($j=0; $j < count($iplist); $j++)
{
if(preg_match('#' . $iplist[$j] . '#i', $_SERVER['REMOTE_ADDR']))
{
// This is where you put what you want to happen
// For now we will just kill the script because I am lazy, you can change it to a redirect later
die('You are banned.');
}
}
?>


Sorry I didnt do any debugging or syntax checking and Yahoo Answers removes extra whitespace so bad formatting. Hopefully I didn't make any mistakes, try it out ;)
 
Back
Top