function ipfilter($ip) {
$source = array("67.197.245.100/16",
"104.151.241.0/16",
"127.0.0.0/8");
foreach ($source as $line) {
// Get the base and the bits from the CIDR
list($base, $bits) = explode('/', $line);
// Now split it up into it's classes
list($a, $b, $c, $d) = explode('.', $base);
// Now do some bit shifting/switching to convert to ints
$i = ($a << 24) + ($b << 16) + ( $c << 8 ) + $d;
$mask = $bits == 0 ? 0: (~0 << (32 - $bits));
// Here's our lowest int
$low = $i & $mask;
// Here's our highest int
$high = $i | (~$mask & 0xFFFFFFFF);
// Now split the ip we're checking against up into classes
list($a, $b, $c, $d) = explode('.', $ip);
// Now convert the ip we're checking against to an int
$check = ($a << 24) + ($b << 16) + ( $c << 8 ) + $d;
// If the ip is within the range, including highest/lowest values,
// then it's witin the CIDR range
if ($check >= $low && $check <= $high) {
return 1;
}
}
return 0;
}