NamePros.Com (http://www.namepros.com/)
-   CODE (http://www.namepros.com/code/)
-   -   Ban Script! Supports specific IPs and even ranges (http://www.namepros.com/code/437508-ban-script-supports-specific-ips-even.html)

Palyriot 02-26-2008 05:41 PM

Ban Script! Supports specific IPs and even ranges
 
This banning script should work for any range in any quartile of the IP. Even [0-255].[0-255].[0-255].[0-255] will work. With every $ip_range value you add, it will only get a very small fraction of a second slower. Running the script with 100 ip ranges took 0.00264000892639 seconds.

Have Fun!
PHP Code:
<?php

function checkban($ip, $banned_ip, $ip_ranges)
{
    
$ipquartiles = explode('.', $ip);
    if (
in_array($ip, $banned_ip))
    {
        return
true;
        
    }
    
    if (
$banned != true)
    {
        foreach(
$ip_ranges as $ip_range)
        {
            
$quartiles = explode('.', $ip_range);
            for (
$i = 0; $i <= 4; $i++)
            {
                if (
preg_match('/\[([0-9]+)\-([0-9]+)\]/', $quartiles[$i], $matches))
                {
                    
$start = $matches[1];
                    
$end = $matches[2];
                    if (
$ipquartiles[$i] < $start || $ipquartiles[$i] > $end)
                    {
                        return
false;
                    }
                    else
                    {
                        
$banned = true;
                    }
                }
                else
                {
                    if (
$quartiles[$i] != $ipquartiles[$i])
                    {
                        return
false;
                    }
                }
            }
        }
    }
    return
$banned;
}

$banned_ip = array();
$banned_ip[] = '204.96.148.3';
$banned_ip[] = '205.162.217.141';

$ip_ranges = array();
$ip_ranges[] = '[0-255].0.0.1';

$ip = $_SERVER['REMOTE_ADDR'];

$banned = checkban($ip, $banned_ip, $ip_ranges);

if (
$banned == true)
{
    echo
'banned!';
    
//header("location: http://www.yahoo.com");
}
else
{
   
// header("location: http://www.google.com");
   
echo 'not banned';
}

?>


All times are GMT -7. The time now is 10:18 PM.
Site Sponsors
Advertise your business at NamePros

Powered by: vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 2.4.0