Forum Moderators: coopster

Message Too Old, No Replies

IPs in array

Add more IPs

         

IngoZ

11:51 am on Mar 10, 2012 (gmt 0)

10+ Year Member



I found a code to block an IP range but don't know how to add more IPs. What I want is to be able to ad multiple IP ranges. Thank you

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

IanKelley

6:20 pm on Mar 10, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What you have isn't really the best solution for blocking IPs, but it isn't the worst either... the following will make it possible to add more IPs to be blocked:


<?php
$blocklist = array(
'123.45.67.89', // Single IP
array('68.61.156.0','61.181.255'), // IP Range
'111.111.111.111' // Another single IP
);
$ip = ip2long($_SERVER['REMOTE_ADDR']);
foreach ($blocklist as $b) {
if (is_array($b)) {
$s = ip2long($b[0]);
$e = ip2long($b[1]);
if ($ip >= $s && $ip <= $e) {
print 'Blocked IP'; exit();
}
}
elseif ($ip == ip2long($b)) {
print 'Blocked IP'; exit();
}
}

?>