Skip to content

Instantly share code, notes, and snippets.

@akeinhell
Created February 11, 2015 09:54
Show Gist options
  • Save akeinhell/bb753db7cc0e5a7d04b2 to your computer and use it in GitHub Desktop.
Save akeinhell/bb753db7cc0e5a7d04b2 to your computer and use it in GitHub Desktop.
Проверка IP В диапазоне
function matchCIDR($addr,$cidr) {
list($ip,$mask) = explode('/',$cidr);
return (ip2long($addr) >> (32 - $mask) == ip2long($ip) >> (32 - $mask));
}
function cidr_match($ip, $range)
{
list ($subnet, $bits) = explode('/', $range);
$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;
}
matchCIDR ("192.168.1.200", "192.168.1.150/25");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment