- Impact
- 10
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!
Have Fun!
PHP:
<?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';
}
?>