0 follower

Class yii\helpers\BaseIpHelper

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

Class BaseIpHelper provides concrete implementation for yii\helpers\IpHelper

Do not use BaseIpHelper, use yii\helpers\IpHelper instead.

Public Methods

Hide inherited methods

MethodDescriptionDefined 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

ConstantValueDescriptionDefined 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

expandIPv6() public static method

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

getIpVersion() public static method

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

inRange() public static method

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
ip2bin() public static method

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