Block IPs and Allow IPs - PHP
Hi ... I'm trying to write a code to allow only 1 IP range and 1 IP address in and ban the rest out. I've got so it bans all the IP's out... but I'm a stuck now... the IP range I want to allow is '150.208.193.*' and '179.155.3.2'
Any help and input would be much appreciated...
Thanks in advanced....
my code so far is :
<?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();
}
|