I came across this php script on the net as a method of banning IP addresses. It compares the IP address of the surfer to a list of banned ip addesses kept in ip.dat. The script works great. What I'm wondering is I maintain 5 separate ban lists (ip.dat, ip1.dat, ip2.dat,ip3.dat,ip4.dat). Rather then replicate the code 5 times to check the surfer ip address against the ip address in each of the 5 lists, is there a way to modify the $IP_array=file('../folder/IP.dat'); line so that it checks all 5 dat files and not just the one. Thank you <?php $IP = $_SERVER['REMOTE_ADDR']; $IP_array = file('../folder/IP.dat'); $IP_array = str_replace("\n", "", $IP_array); $IP_array = str_replace("\r", "", $IP_array); foreach ($IP_array as $IP_test) { if (substr_count($IP, $IP_test) != "0") { echo 'Banned World!'; die(); } } echo 'Hello World!'; ?>
Why not combine the .dat files into one file? To me that would be the most straightforward way of getting the result you are looking for. Mike
Depending on how many IP addresses you intend on banning, this could get pretty slow, pretty quickly. If I were to do this, I'd set up a database table myself...
If you care about speed at all or get a good number of visits I would definitely place them in a db table with that field indexed for quick searching.
That's a very good point that I never thought about. I think down the road, I might migrate to a database approach if this method does seem to slow. For the person who was asking, why not consolidate all the ban files into a single one, I could do that, but really the preference is to keep them separate as each file represents a specific category of IP addresses. If there is an easy way to modify the original script so that it reads all the input files, without having to replicate the code multiple times, I'd be all ears. Thanks
I have very good perfomance and banning protection using this script: <? $blocked = array( "127.0.0.2", "62.156.152.71", ); for ($i=0; $i<sizeof($blocked); $i++) { if ("$blocked[$i]" == "$REMOTE_ADDR") { $ip = $blocked[$i]; $block = 1; } } ?> Code (markup): note from the author:
I use the following function to ban IPs function check_ip_ban($ipaddress) { global $options; $ip_banned = FALSE; # # checks if the user's IP address is in IP banned address list # if, is then, IP bans # if (!empty($ipaddress)) { if (strpos($options['ip_banning'], $ipaddress) !== FALSE) { $ip_banned = TRUE; } else { # full IP isn't banned, checking if parts are $ip_parts = explode('.', trim($ipaddress)); if (strpos($options['ip_banning'], $ip_parts[0].'.*.*.*') !== FALSE) $ip_banned = TRUE; elseif (strpos($options['ip_banning'], $ip_parts[0].'.'.$ip_parts[1].'.*.*') !== FALSE) $ip_banned = TRUE; elseif (strpos($options['ip_banning'], $ip_parts[0].'.'.$ip_parts[1].'.'.$ip_parts[2].'.*') !== FALSE) $ip_banned = TRUE; } # end if full IP address isn't banned check } return $ip_banned; } Code (markup): where $options['ip_banning'] holds a list of banned addresses like this: # ______________ # | 223.12.13.13 | # | 223.12.34.* | # | 223.12.*.* | # | 223.*.*.* | # |______________| # Code (markup): As for your question: Simply loop the script! foreach (array(ip.dat, ip1.dat, ip2.dat,ip3.dat,ip4.dat) AS $filename) { // include script here, except replace IP.dat with $filename } Code (markup):
Edmunds, Would you be able to confirm the syntax below. I did make the changes you suggested and received the error message "Parse error: syntax error, unexpected '}' in C:\servers\xampp\htdocs\test.php on line 20" <?php foreach (array(ip.dat, ip1.dat, ip2.dat,ip3.dat,ip4.dat) AS $filename) $IP = $_SERVER['REMOTE_ADDR']; $IP_array = file('$filename'); $IP_array = str_replace("\n", "", $IP_array); $IP_array = str_replace("\r", "", $IP_array); foreach ($IP_array as $IP_test) { if (substr_count($IP, $IP_test) != "0") { echo 'Banned World!'; die(); } } echo 'Hello World!'; } ?>
Try putting in some ' ... eg foreach (array('ip.dat', 'ip1.dat', 'ip2.dat','ip3.dat','ip4.dat') AS $filename) (note: havn't tested this)
<?php foreach (array('ip.dat', 'ip1.dat', 'ip2.dat', 'ip3.dat', 'ip4.dat') AS $filename) { $IP = $_SERVER['REMOTE_ADDR']; $IP_array = file('$filename'); $IP_array = str_replace("\n", "", $IP_array); $IP_array = str_replace("\r", "", $IP_array); foreach ($IP_array as $IP_test) { if (substr_count($IP, $IP_test) != "0") { echo 'Banned World!'; die(); } } echo 'Hello World!'; } ?> Code (markup): Should be correct.