Liam Delahunty: Home Tips Web Contact
Recommended laptop
under £500
.

Think I deserve a present? See my Amazon Wish List

Ban Access from an IP address with PHP

In the war against terror, or at least against spam bots that index pages and/or attempt to send hundreds of comments using forms, I've created this simple check to inhibit their access of the site. I include this on every page at the very top before anything else is printed. Sending the bot a 404 will hopefully de-index the page in the long term, but I wouldn't hold my breath.

// banned list
$banned[]="123.456.78.90"; // specific IP to ban
$banned[]="123.456.78."; // Perhaps a whole C-Class
$banned[]="123.456."; // 65,534 addresses to ban!
$banned[]="123."; //  16,777,214 addresses to prohibit
if($banned){
	foreach ($banned AS $bad_ip){
		if(eregi("^$bad_ip",$_SERVER['REMOTE_ADDR'])){
			header("HTTP/1.0 404 Not Found");
			// 	or perhaps send them somewhere to record the attempt
			//	header("Location: http://www.example.com/bad_ip.php");
			exit();
		}
	}
}
// rest of the normal page

See also PHP gethostbyaddr and PHP checkdnsrr.

Share this!  



Php Prev Next Array | Php Prohibit Access To Pages Based On Ip Address | Php Read Titles


 

  Tips | Home | Search | Contact

Link here: