Verifying IP Address Inclusion in a CIDR Subnet
To determine whether an IPv4 address falls within a specified CIDR subnet, a straightforward method involves the following steps:
Conversion to Long Integers:
Subnet Mask Derivation:
Bitwise Comparison:
Implementation:
The following PHP function encapsulates this logic:
function cidr_match($ip, $range)
{
list ($subnet, $bits) = explode('/', $range);
if ($bits === null) {
$bits = 32;
}
$ip = ip2long($ip);
$subnet = ip2long($subnet);
$mask = -1
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3