0 follower

Class yii\helpers\IpHelper

Inheritanceyii\helpers\IpHelper » yii\helpers\BaseIpHelper
Available since version2.0.14
Source Code https://github.com/yiisoft/yii2/blob/master/framework/helpers/IpHelper.php

Class IpHelper provides a set of IP-related static methods.

Methods expect correct IP addresses. To validate IP addresses use IpValidator.

Public Methods

Hide inherited methods

Method Description Defined By
expandIPv6() Expands an IPv6 address to it's full notation. yii\helpers\BaseIpHelper
getIpVersion() Gets the IP version. Does not perform IP address validation. yii\helpers\BaseIpHelper
inRange() Checks whether IP address or subnet $subnet is contained by $subnet. yii\helpers\BaseIpHelper
ip2bin() Converts IP address to bits representation. yii\helpers\BaseIpHelper

Constants

Hide inherited constants

Constant Value Description Defined By
IPV4 4 yii\helpers\BaseIpHelper
IPV4_ADDRESS_LENGTH 32 The length of IPv4 address in bits yii\helpers\BaseIpHelper
IPV6 6 yii\helpers\BaseIpHelper
IPV6_ADDRESS_LENGTH 128 The length of IPv6 address in bits yii\helpers\BaseIpHelper

Method Details

Hide inherited methods

expandIPv6() public static method

Defined in: yii\helpers\BaseIpHelper::expandIPv6()

Expands an IPv6 address to it's full notation.

For example 2001:db8::1 will be expanded to 2001:0db8:0000:0000:0000:0000:0000:0001

public static string expandIPv6 ( $ip )
$ip string

The original valid IPv6 address

return string

The expanded IPv6 address

                public static function expandIPv6($ip)
{
    $hex = unpack('H*hex', inet_pton($ip));
    return substr(preg_replace('/([a-f0-9]{4})/i', '$1:', $hex['hex']), 0, -1);
}

            
getIpVersion() public static method

Defined in: yii\helpers\BaseIpHelper::getIpVersion()

Gets the IP version. Does not perform IP address validation.

public static integer getIpVersion ( $ip )
$ip string

The valid IPv4 or IPv6 address.

return integer

IPV4 or IPV6

                public static function getIpVersion($ip)
{
    return strpos($ip, ':') === false ? self::IPV4 : self::IPV6;
}

            
inRange() public static method

Defined in: yii\helpers\BaseIpHelper::inRange()

Checks whether IP address or subnet $subnet is contained by $subnet.

For example, the following code checks whether subnet 192.168.1.0/24 is in subnet 192.168.0.0/22:

IpHelper::inRange('192.168.1.0/24', '192.168.0.0/22'); // true

In case you need to check whether a single IP address 192.168.1.21 is in the subnet 192.168.1.0/24, you can use any of theses examples:

IpHelper::inRange('192.168.1.21', '192.168.1.0/24'); // true
IpHelper::inRange('192.168.1.21/32', '192.168.1.0/24'); // true

See also https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing.

public static boolean inRange ( $subnet, $range )
$subnet string

The valid IPv4 or IPv6 address or CIDR range, e.g.: 10.0.0.0/8 or 2001:af::/64

$range string

The valid IPv4 or IPv6 CIDR range, e.g. 10.0.0.0/8 or 2001:af::/64

return boolean

Whether $subnet is contained by $range

throws yii\base\NotSupportedException

                public static function inRange($subnet, $range)
{
    list($ip, $mask) = array_pad(explode('/', $subnet), 2, null);
    list($net, $netMask) = array_pad(explode('/', $range), 2, null);
    $ipVersion = static::getIpVersion($ip);
    $netVersion = static::getIpVersion($net);
    if ($ipVersion !== $netVersion) {
        return false;
    }
    $maxMask = $ipVersion === self::IPV4 ? self::IPV4_ADDRESS_LENGTH : self::IPV6_ADDRESS_LENGTH;
    $mask = isset($mask) ? $mask : $maxMask;
    $netMask = isset($netMask) ? $netMask : $maxMask;
    $binIp = static::ip2bin($ip);
    $binNet = static::ip2bin($net);
    return substr($binIp, 0, $netMask) === substr($binNet, 0, $netMask) && $mask >= $netMask;
}

            
ip2bin() public static method

Defined in: yii\helpers\BaseIpHelper::ip2bin()

Converts IP address to bits representation.

public static string ip2bin ( $ip )
$ip string

The valid IPv4 or IPv6 address

return string

Bits as a string

throws yii\base\NotSupportedException

                public static function ip2bin($ip)
{
    $ipBinary = null;
    if (static::getIpVersion($ip) === self::IPV4) {
        $ipBinary = pack('N', ip2long($ip));
    } elseif (@inet_pton('::1') === false) {
        throw new NotSupportedException('IPv6 is not supported by inet_pton()!');
    } else {
        $ipBinary = inet_pton($ip);
    }
    $result = '';
    for ($i = 0, $iMax = strlen($ipBinary); $i < $iMax; $i += 4) {
        $result .= str_pad(decbin(unpack('N', substr($ipBinary, $i, 4))[1]), 32, '0', STR_PAD_LEFT);
    }
    return $result;
}