IpHelper
extends BaseIpHelper
in package
Class IpHelper provides a set of IP-related static methods.
Methods expect correct IP addresses. To validate IP addresses use [[\yii\validators\IpValidator|IpValidator]].
Tags
Table of Contents
Constants
- IPV4 = 4
- IPV4_ADDRESS_LENGTH = 32
- The length of IPv4 address in bits
- IPV6 = 6
- IPV6_ADDRESS_LENGTH = 128
- The length of IPv6 address in bits
Methods
- expandIPv6() : string
- Expands an IPv6 address to it's full notation.
- getIpVersion() : int
- Gets the IP version. Does not perform IP address validation.
- inRange() : bool
- Checks whether IP address or subnet $subnet is contained by $subnet.
- ip2bin() : string
- Converts IP address to bits representation.
Constants
IPV4
public
mixed
IPV4
= 4
IPV4_ADDRESS_LENGTH
The length of IPv4 address in bits
public
mixed
IPV4_ADDRESS_LENGTH
= 32
IPV6
public
mixed
IPV6
= 6
IPV6_ADDRESS_LENGTH
The length of IPv6 address in bits
public
mixed
IPV6_ADDRESS_LENGTH
= 128
Methods
expandIPv6()
Expands an IPv6 address to it's full notation.
public
static expandIPv6(string $ip) : string
For example 2001:db8::1
will be expanded to 2001:0db8:0000:0000:0000:0000:0000:0001
Parameters
- $ip : string
-
the original valid IPv6 address
Return values
string —the expanded IPv6 address
getIpVersion()
Gets the IP version. Does not perform IP address validation.
public
static getIpVersion(string $ip) : int
Parameters
- $ip : string
-
the valid IPv4 or IPv6 address.
Return values
int —[[IPV4]] or [[IPV6]]
inRange()
Checks whether IP address or subnet $subnet is contained by $subnet.
public
static inRange(string $subnet, string $range) : bool
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
- $subnet : string
-
the valid IPv4 or IPv6 address or CIDR range, e.g.:
10.0.0.0/8
or2001:af::/64
- $range : string
-
the valid IPv4 or IPv6 CIDR range, e.g.
10.0.0.0/8
or2001:af::/64
Tags
Return values
bool —whether $subnet is contained by $range
ip2bin()
Converts IP address to bits representation.
public
static ip2bin(string $ip) : string
Parameters
- $ip : string
-
the valid IPv4 or IPv6 address
Tags
Return values
string —bits as a string