Online Solutions Development Blog   |  

RSS

Simple script to ban an IP address

posted by ,
Categories: PHP
Taggs , , ,

Below you can find a simple php script that blocks ip addresses. It uses a function (called iplong) that converts the standard IPv4 address to a long int value and the main function (ban) that checks if a provided ip can be found in a list of blocked addresses. Hope you will find this useful.

function ban($range = array(), $ip = '')
{
        $ip = longip(trim($ip));
        if($ip == FALSE)
        {
                return FALSE;
        }
        if(empty($ip))
        {
                return FALSE;
        }
        foreach($range AS $key => $val)
        {
                $temp = explode('-', $val);
                if(empty($temp[0]))
                {
                        return FALSE;
                }
                else
                {
                        $start_ip = longip(trim($temp[0]));
                        if($start_ip == FALSE)
                        {
                                return FALSE;
                        }
                }
                if(empty($temp[1]))
                {
                        if($ip == $start_ip)
                        {
                                return TRUE;
                        }
                }
                else
                {
                        $stop_ip = longip(trim($temp[1]));
                        if($stop_ip == FALSE)
                        {
                                return FALSE;
                        }
                }
                if($start_ip <= $ip && $ip <= $stop_ip)
                {
                        return TRUE;
                }
        }
        return FALSE;
}
function longip($ip)
{
        if(empty($ip))
        {
                return FALSE;
        }
        $block = explode('.', $ip);
        if(count($block) != 4)
        {
                return FALSE;
        }
        $i = 3;
        $block_ip = 0;
        foreach($block as $k => $v)
        {
                $v = filter_var($v, FILTER_VALIDATE_INT);
                if($v < 0)
                {
                        $v = 0;
                }
                if($v > 255)
                {
                        $v = 255;
                }
                $block_ip += pow(256, $i)*$v;
                $i--;
        }
        return $block_ip;
}

//usage
$block_range = array(   '192.168.1.0 - 192.168.2.255',
                        '192.168.4.0 - 192.168.4.255',
                        '192.168.6.1'
                );

$ip = '192.168.3.255';

if(ban($block_range, '192.168.6.1'))
{
        echo "BAN\n";
}
else
{
        echo "UNBAN\n";
}

If you liked this post
you can buy me a beer

One Response to Simple script to ban an IP address

Add a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Also, if you want to display source code you can enclose it between [html] and [/html], [js] and [/js], [php] and [/php] etc