Skip to content
PHP Ecosystem
Explore

BaseIpHelper

API reference › yii › helpers › BaseIpHelper

classyii\helpers\BaseIpHelper
Since2.0.14
Sourceframework/helpers/BaseIpHelper.php
1 subclass

Class BaseIpHelper provides concrete implementation for IpHelper

Do not use BaseIpHelper, use IpHelper instead.

MethodDescriptionDefined by
expandIPv6() staticExpands an IPv6 address to it’s full notation.BaseIpHelper
getIpVersion() staticGets the IP version. Does not perform IP address validation.BaseIpHelper
inRange() staticChecks whether IP address or subnet $subnet is contained by $subnet.BaseIpHelper
ip2bin() staticConverts IP address to bits representation.BaseIpHelper
ConstantValueDescriptionDefined by
IPV44BaseIpHelper
IPV66BaseIpHelper
IPV6_ADDRESS_LENGTH128The length of IPv6 address in bitsBaseIpHelper
IPV4_ADDRESS_LENGTH32The length of IPv4 address in bitsBaseIpHelper
public static function expandIPv6(string $ip): string

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

Parameters

ParameterTypeDescription
$ipstringThe original valid IPv6 address

Returns

string: The expanded IPv6 address

public static function getIpVersion(string $ip): int

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

Parameters

ParameterTypeDescription
$ipstringThe valid IPv4 or IPv6 address.

Returns

int: IPV4 or IPV6

public static function inRange(string $subnet, string $range): bool

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

Parameters

ParameterTypeDescription
$subnetstringThe valid IPv4 or IPv6 address or CIDR range, e.g.: 10.0.0.0/8 or 2001:af::/64
$rangestringThe 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

public static function ip2bin(string $ip): string

Converts IP address to bits representation.

Parameters

ParameterTypeDescription
$ipstringThe valid IPv4 or IPv6 address

Returns

string: Bits as a string

Throws