<?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'));
- Output for 7.4.0 - 7.4.33, 8.0.1 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.26, 8.4.1 - 8.4.13
- bool(true)
bool(false)
preferences:
105.68 ms | 410 KiB | 6 Q