BaseIpHelper
API reference › yii › helpers › BaseIpHelper
1 subclass
Class BaseIpHelper provides concrete implementation for IpHelper
Do not use BaseIpHelper, use IpHelper instead.
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
expandIPv6() static | Expands an IPv6 address to it’s full notation. | BaseIpHelper |
getIpVersion() static | Gets the IP version. Does not perform IP address validation. | BaseIpHelper |
inRange() static | Checks whether IP address or subnet $subnet is contained by $subnet. | BaseIpHelper |
ip2bin() static | Converts IP address to bits representation. | BaseIpHelper |
Constants
Section titled “Constants”| Constant | Value | Description | Defined by |
|---|---|---|---|
IPV4 | 4 | BaseIpHelper | |
IPV6 | 6 | BaseIpHelper | |
IPV6_ADDRESS_LENGTH | 128 | The length of IPv6 address in bits | BaseIpHelper |
IPV4_ADDRESS_LENGTH | 32 | The length of IPv4 address in bits | BaseIpHelper |
Method details
Section titled “Method details”expandIPv6()
Section titled “expandIPv6() ”public static function expandIPv6(string $ip): stringExpands 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
Parameters
| Parameter | Type | Description |
|---|---|---|
$ip | string | The original valid IPv6 address |
Returns
string: The expanded IPv6 address
getIpVersion()
Section titled “getIpVersion() ”public static function getIpVersion(string $ip): intGets the IP version. Does not perform IP address validation.
inRange()
Section titled “inRange() ”public static function inRange(string $subnet, string $range): boolChecks 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'); // trueIn 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'); // trueIpHelper::inRange('192.168.1.21/32', '192.168.1.0/24'); // trueParameters
| Parameter | Type | Description |
|---|---|---|
$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 |
Returns
bool: Whether $subnet is contained by $range
Throws
See also
ip2bin()
Section titled “ip2bin() ”public static function ip2bin(string $ip): stringConverts IP address to bits representation.
Parameters
| Parameter | Type | Description |
|---|---|---|
$ip | string | The valid IPv4 or IPv6 address |
Returns
string: Bits as a string
Throws