Class yii\helpers\BaseIpHelper
Inheritance | yii\helpers\BaseIpHelper |
---|---|
Subclasses | yii\helpers\IpHelper |
Available since version | 2.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
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
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
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 |
---|
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 |
---|
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.: |
$range | string | The valid IPv4 or IPv6 CIDR range, e.g. |
return | boolean | Whether $subnet is contained by $range |
---|---|---|
throws | yii\base\NotSupportedException |
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 |