homepage Welcome to WebmasterWorld Guest from 54.197.111.87
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
IPs in array
Add more IPs
IngoZ




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

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




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

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();
}
}

?>

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved