sammo_ti Posted March 13, 2008 Share Posted March 13, 2008 Hi, I'm a little stuck... I've been working on a code to ban all IP's except '150.208.193.*' range and '179.155.3.2'. I've gotten it to ban all IP's so far... Any help will be appreciated or better ways of doing it... Thanks in advanced... <?php //$valid_ips[] = '150.208.193.*'; //$valid_ips[] = '179.155.3.2'; $banned_ip[] = '*.*.*.*'; function is_banned($ip) { $pattern = str_replace(array('*', '.'), array('\d+', '\.'), $ip); return preg_match("~^{$pattern}$~", $_SERVER['REMOTE_ADDR']); } if (array_filter($banned_ip, 'is_banned')) { echo 'Banned'; exit(); } else { echo 'Not banned'; exit(); } Link to comment https://forums.phpfreaks.com/topic/95922-allow-ip-and-ban-the-rest/ Share on other sites More sharing options...
Daniel0 Posted March 14, 2008 Share Posted March 14, 2008 Try this: <?php $banned_ips = array( '150.208.193.*', '179.155.3.2', ); $user_ip = $_SERVER['REMOTE_ADDR']; $is_allowed = false; foreach ($banned_ips as $ip) { $octets = explode('.', $ip); $num_octets = count($octets); if($num_octets > 4) continue; // malformed IP address $ip .= str_repeat('.*', 4 - $num_octets); $ip = str_replace('*', '[0-9]{1,3}', $ip); if (preg_match('#^' . $ip . '$#', $user_ip)) { $is_allowed = true; break; // no need to check the remaining addresses } } if (!$is_allowed) { die('Sorry, you are not allowed to view this page.'); } ?> Link to comment https://forums.phpfreaks.com/topic/95922-allow-ip-and-ban-the-rest/#findComment-492011 Share on other sites More sharing options...
jiggles Posted March 16, 2008 Share Posted March 16, 2008 That' is awesome dude.... Your the best mate !!! thanks a bundle .... Link to comment https://forums.phpfreaks.com/topic/95922-allow-ip-and-ban-the-rest/#findComment-493660 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.