Showing entries with tag "subnet".

Found 1 entries

PHP: Determine if an IP address is part of a given subnet

Given an IP address I need to determine if it is part of a subnet for whitelist/blacklist purposes.

$allowed = ip_in_subnet('65.182.224.40','65.182.224.0/24');
// Borrowed from http://php.net/manual/en/function.ip2long.php#82397
function ip_in_subnet($ip, $cidr) {
    list ($net, $mask) = explode ('/', $cidr);
    return ( ip2long ($ip) & ~((1 << (32 - $mask)) - 1) ) == ip2long ($net);
}
Leave A Reply