tags:

views:

92

answers:

3
+6  A: 

The best way to check IP ranges is to convert the dotted address into a 32-bit number and perform comparisons on that. The ip2long function can do the conversion for you. For example:

$range_start = ip2long("68.61.156.0");
$range_end   = ip2long("68.61.181.255");
$ip          = ip2long($_SERVER['REMOTE_ADDR']);
if ($ip >= $range_start && $ip <= $range_end) {
  // blocked
}

You can put several of these ranges into an array and iterate over it to check multiple ranges.

casablanca
A: 

If you're willing to use SQL, and have a table of IP ranges,

SELECT * FROM `ips` WHERE $ip BETWEEN `start` AND `end`

If you get zero results, then it's not blocked.

EDIT: Using the ip2long function, of course.

This is a better way if you have a lot of random ranges; a pure PHP way is better for fewer.

zebediah49
A: 

Checkout my previous answer with examples.

Marcus Adams