Skip to content

Instantly share code, notes, and snippets.

@chaoswey
Last active July 22, 2019 01:47
Show Gist options
  • Save chaoswey/3764cc33ba07826416b972e10bd486be to your computer and use it in GitHub Desktop.
Save chaoswey/3764cc33ba07826416b972e10bd486be to your computer and use it in GitHub Desktop.
網路遮罩
function ip_in_range($ip, $range)
{
if (strpos($range, '/') == false) {
return ($ip == $range);
}
list($range, $netmask) = explode('/', $range, 2);
$range_decimal = ip2long($range);
$ip_decimal = ip2long($ip);
$wildcard_decimal = pow(2, (32 - $netmask)) - 1;
$netmask_decimal = ~$wildcard_decimal;
return (($ip_decimal & $netmask_decimal) == ($range_decimal & $netmask_decimal));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment