- var_dump: documentation ( source)
- explode: documentation ( source)
- ip2long: documentation ( source)
<?php
function cidr_match($ip, $range){
list ($subnet, $bits) = explode('/', $range);
if ($bits === null) {
$bits = 32;
}
$ip = ip2long($ip);
$subnet = ip2long($subnet);
$mask = -1 << (32 - $bits);
$subnet &= $mask; # nb: in case the supplied subnet wasn't correctly aligned
return ($ip & $mask) == $subnet;
}
var_dump(cidr_match('79.142.17.1', '79.142.16.0/20'));
var_dump(cidr_match('79.142.35.4', '79.142.16.0/20'));