You are here

private static function Database::ipVersionAndNumber in Smart IP 6.2

Same name and namespace in other branches
  1. 7.2 includes/vendor/ip2location/ip2location-php/IP2Location.php \IP2Location\Database::ipVersionAndNumber()

Get the IP version and number of the given IP address

This method will return an array, whose components will be:

  • first: 4 if the given IP address is an IPv4 one, 6 if it's an IPv6 one, or fase if it's neither.
  • second: the IP address' number if its version is 4, the number string if its version is 6, false otherwise.

@access private @static

Parameters

string $ip IP address to extract the version and number for:

Return value

array

1 call to Database::ipVersionAndNumber()
Database::lookup in includes/vendor/ip2location/ip2location-php/IP2Location.php
This function will look the given IP address up in the database and return the result(s) asked for

File

includes/vendor/ip2location/ip2location-php/IP2Location.php, line 985

Class

Database
IP2Location database class

Namespace

IP2Location

Code

private static function ipVersionAndNumber($ip) {
  if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
    return [
      4,
      sprintf('%u', ip2long($ip)),
    ];
  }
  elseif (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
    $result = 0;
    foreach (str_split(bin2hex(inet_pton($ip)), 8) as $word) {
      $result = bcadd(bcmul($result, '4294967296', 0), self::wrap32(hexdec($word)), 0);
    }
    return [
      6,
      $result,
    ];
  }
  else {

    // Invalid IP address, return falses
    return [
      false,
      false,
    ];
  }
}