Imbo\EventListener\StatsAccess::cidr4Match PHP Method

cidr4Match() private method

Check an IPv4 address is in a subnet
private cidr4Match ( string $ip, string $range ) : boolean
$ip string The IP address to check (for instance 192.168.1.10)
$range string A CIDR notated IP address and routing prefix (for instance 192.168.1.0/24)
return boolean
    private function cidr4Match($ip, $range)
    {
        // Split CIDR on /
        list($subnet, $bits) = explode('/', $range);
        // Convert ip's to long
        $ip = ip2long($ip);
        $subnet = ip2long($subnet);
        // Generate mask and align the subnet if necessary
        $mask = -1 << 32 - $bits;
        $subnet &= $mask;
        // Check for match
        return ($ip & $mask) === $subnet;
    }