Attached. In the file ips.txt you have to put the banned ips. If you want, I can make a system to administrate banned IPs via web, and encrypt the file ips.txt.
A slight extension of mgware's script, this allows wildcard banning with * characters (eg 63.62.32.* bans 63.62.32.0-63.62.32.255), and can be included at the top of your PHP page like (provided you saved the below code as ipcheck.php obviously): include 'ipcheck.php'; <? if (file_exists("ips.txt")) { $ipsBanned = file_get_contents("ips.txt"); $ipsBanned = str_replace(array("\r\n","\n",".","*","()?"), array(")?(",")?(","\\.","[\\d\\.]*",""),"^(".$ipsBanned.")?$"); $ip = $HTTP_SERVER_VARS["REMOTE_ADDR"]; if (preg_match($ipsBanned,$ip)){ die("Banned IP."); } ?> PHP:
Only a TIP: When you detect a banned IP, you should not return a 404 HTTP error, nor even a "Banned IP" message. That way you're telling the banned user that you catch them, and then he/she can access to your site using a proxy. Is better if you display a generic page with no useful info.
Instaed of having the IP lists in a txt file, I am storing them in a database table as a serialized array. This is how it looks like: s:8:"ip_block";a:2:{i:0;s:9:"127.0.0.2";i:1;s:12:"129.23.23.45";} PHP: Now, I call the database on top of the file and then add: $ipsBanned = $CFG['ip_block']; PHP: How do I proceed from here? I want to use ds316's code above. It works perfectly if from a text file, I am just stuck as I want it with a database... This part needs some modification: $ipsBanned = str_replace(array("\r\n","\n",".","*","()?"), array(")?(",")?(","\\.","[\\d\\.]*",""),"^(".$ipsBanned.")?$"); $ip = $HTTP_SERVER_VARS["REMOTE_ADDR"]; if (preg_match($ipsBanned,$ip)){ die("Banned IP."); } PHP: I appreciate any clues.
After $ipsBanned = $CFG['ip_block']; PHP: add and replace $ipsBanned = unserialize($ipsBanned); $ipsBanned = implode(")?(",$ipsBanned); $ipsBanned = str_replace(array(".","*","()?"), array("\\.","[\\d\\.]*",""),"^(".$ipsBanned.")?$"); $ip = $HTTP_SERVER_VARS["REMOTE_ADDR"]; if (preg_match($ipsBanned,$ip)){ die("Banned IP."); } PHP: Assuming you haven't unserialized the data beforehand.
Thank you ds316! The above code was not really perfect, but I contacted ds316 by PM and it was fixed to my satisfaction. He was quick and neat. Thank you once again. medusa
use $_SERVER['REMOTE_ADDR'] instead. And just check like this: if($ip == $array_from_db){ display_generic_page(); }