Last active
July 22, 2019 01:47
-
-
Save chaoswey/3764cc33ba07826416b972e10bd486be to your computer and use it in GitHub Desktop.
網路遮罩
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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