September 29th, 2006

Ban visitors based on IP address with PHP

Most web hosting control panels allow you to ban IP addresses or ranges from your website. But if you don't have that capability provided by your host, here's the simple way to enable this through PHP:

$banip = array('127.0.0.1','127.0.0.2','127.0.0.3');
 
$ip = $_SERVER['REMOTE_ADDR'];
 
for($i=0;$i<3;$i++){
    if($ip == $banip[$i]) {
        echo "You are currently banned from this website, sorry!";
        die();
    } else {
        // carry on with site rendering
    }
}

Leave a reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-Spam Image