homepage Welcome to WebmasterWorld Guest from 184.72.184.104
register, login, search, subscribe, help, library, PubCon, announcements, recent posts, open posts,
Pubcon Platinum Sponsor
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library : Charter : Moderators: coopster & eelixduppy & 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.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
WebmasterWorld ® and PubCon ® are a Registered Trademarks of Pubcon Inc.
© Pubcon Inc. 1996-2012 all rights reserved