You are here

public static function IpUtils::checkIp4 in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/http-foundation/IpUtils.php \Symfony\Component\HttpFoundation\IpUtils::checkIp4()

Compares two IPv4 addresses. In case a subnet is given, it checks if it contains the request IP.

Parameters

string $requestIp IPv4 address to check:

string $ip IPv4 address or subnet in CIDR notation:

Return value

bool Whether the request IP matches the IP, or whether the request IP is within the CIDR subnet.

File

vendor/symfony/http-foundation/IpUtils.php, line 62

Class

IpUtils
Http utility functions.

Namespace

Symfony\Component\HttpFoundation

Code

public static function checkIp4($requestIp, $ip) {
  if (false !== strpos($ip, '/')) {
    list($address, $netmask) = explode('/', $ip, 2);
    if ($netmask === '0') {

      // Ensure IP is valid - using ip2long below implicitly validates, but we need to do it manually here
      return filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
    }
    if ($netmask < 0 || $netmask > 32) {
      return false;
    }
  }
  else {
    $address = $ip;
    $netmask = 32;
  }
  return 0 === substr_compare(sprintf('%032b', ip2long($requestIp)), sprintf('%032b', ip2long($address)), 0, $netmask);
}