Documentation

IpUtils
in package

FinalYes

IP address utility class.

Provides IP address validation, format detection (IPv4/IPv6), CIDR range checking, and client IP extraction from X-Forwarded-For header.

Table of Contents

Properties

$cache  : array<string|int, mixed>
Cache verified IP results. To reduce load.

Methods

getClientIpFromXFF()  : string|null
Get client ip from X-Forwarded-For.
inRange()  : bool
Checks if an IPv4 or IPv6 address is contained in the list of given IPs or subnets.
inRangeIPv4()  : bool
Compares two IPv4 addresses. In case a subnet is given, it checks if it contains the request IP.
inRangeIPv6()  : bool
Compares two IPv6 addresses. In case a subnet is given, it checks if it contains the request IP.
isIPv4()  : bool
Check if it is IPv4 format.
isIPv6()  : bool
Check if it is IPv6 format.

Properties

$cache

Cache verified IP results. To reduce load.

private static array<string|int, mixed> $cache = []

Methods

getClientIpFromXFF()

Get client ip from X-Forwarded-For.

public static getClientIpFromXFF() : string|null
use \X\Util\IpUtils;

IpUtils::getClientIpFromXFF();// => 202.210.220.78
Return values
string|null —

Client IP.

inRange()

Checks if an IPv4 or IPv6 address is contained in the list of given IPs or subnets.

public static inRange(string $value, string|array<string|int, string> $allowables) : bool
use \X\Util\IpUtils;

// Allowable:202.210.220.64/28
IpUtils::inRange('202.210.220.63', '202.210.220.64/28');// false
IpUtils::inRange('202.210.220.64', '202.210.220.64/28');// true
IpUtils::inRange('202.210.220.65', '202.210.220.64/28');// true
IpUtils::inRange('202.210.220.78', '202.210.220.64/28');// true
IpUtils::inRange('202.210.220.79', '202.210.220.64/28');// true
IpUtils::inRange('202.210.220.80', '202.210.220.64/28');// false

// Allowable:192.168.1.0/24
IpUtils::inRange('192.168.0.255', '192.168.1.0/24'); // false
IpUtils::inRange('192.168.1.0', '192.168.1.0/24'); // true
IpUtils::inRange('192.168.1.1', '192.168.1.0/24'); // true
IpUtils::inRange('192.168.1.244', '192.168.1.0/24'); // true
IpUtils::inRange('192.168.1.255', '192.168.1.0/24'); // true
IpUtils::inRange('192.168.2.0', '192.168.1.0/24'); // false

// Allowable:118.238.251.130
IpUtils::inRange('118.238.251.129', '118.238.251.130'); // false
IpUtils::inRange('118.238.251.130', '118.238.251.130'); // true
IpUtils::inRange('118.238.251.131', '118.238.251.130'); // false

// Allowable:118.238.251.130/32
IpUtils::inRange('118.238.251.129', '118.238.251.130/32'); // false
IpUtils::inRange('118.238.251.130', '118.238.251.130/32'); // true
IpUtils::inRange('118.238.251.131', '118.238.251.130/32'); // false

// Allowable:2001:4860:4860::8888/32
IpUtils::inRange('2001:4859:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF', '2001:4860:4860::8888/32');// false
IpUtils::inRange('2001:4860:4860:0000:0000:0000:0000:8888', '2001:4860:4860::8888/32');// true
IpUtils::inRange('2001:4860:4860:0000:0000:0000:0000:8889', '2001:4860:4860::8888/32');// true
IpUtils::inRange('2001:4860:FFFF:FFFF:FFFF:FFFF:FFFF:FFFE', '2001:4860:4860::8888/32');// true
IpUtils::inRange('2001:4860:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF', '2001:4860:4860::8888/32');// true
IpUtils::inRange('2001:4861:0000:0000:0000:0000:0000:0000', '2001:4860:4860::8888/32');// false

// Allowable:2404:7a81:b0a0:9100::/64
IpUtils::inRange('2404:7A81:B0A0:90FF:0000:0000:0000:0000', '2404:7A81:B0A0:9100::/64');// false
IpUtils::inRange('2404:7A81:B0A0:9100:0000:0000:0000:0000', '2404:7A81:B0A0:9100::/64');// true
IpUtils::inRange('2404:7A81:B0A0:9100:0000:0000:0000:0001', '2404:7A81:B0A0:9100::/64');// true
IpUtils::inRange('2404:7A81:B0A0:9100:A888:5EE2:EA92:B618', '2404:7A81:B0A0:9100::/64');// true
IpUtils::inRange('2404:7A81:B0A0:9100:D03:959E:7F47:9B77', '2404:7A81:B0A0:9100::/64');// true
IpUtils::inRange('2404:7A81:B0A0:9100:FFFF:FFFF:FFFF:FFFE', '2404:7A81:B0A0:9100::/64');// true
IpUtils::inRange('2404:7A81:B0A0:9100:FFFF:FFFF:FFFF:FFFF', '2404:7A81:B0A0:9100::/64');// true
IpUtils::inRange('2404:7A81:B0A0:9101:0000:0000:0000:0000', '2404:7A81:B0A0:9100::/64');// false
Parameters
$value : string

IP to be checked.

$allowables : string|array<string|int, string>

List of IPs or subnets (can be a string if only a single one)

Return values
bool —

Whether the IP is valid.

inRangeIPv4()

Compares two IPv4 addresses. In case a subnet is given, it checks if it contains the request IP.

public static inRangeIPv4(string $value, string $allowable) : bool
use \X\Util\IpUtils;

// Allowable:202.210.220.64/28
IpUtils::inRangeIPv4('202.210.220.63', '202.210.220.64/28')// false
IpUtils::inRangeIPv4('202.210.220.64', '202.210.220.64/28')// true
IpUtils::inRangeIPv4('202.210.220.65', '202.210.220.64/28')// true
IpUtils::inRangeIPv4('202.210.220.78', '202.210.220.64/28')// true
IpUtils::inRangeIPv4('202.210.220.79', '202.210.220.64/28')// true
IpUtils::inRangeIPv4('202.210.220.80', '202.210.220.64/28')// false

// Allowable:192.168.1.0/24
IpUtils::inRangeIPv4('192.168.0.255', '192.168.1.0/24');// false
IpUtils::inRangeIPv4('192.168.1.0', '192.168.1.0/24');// true
IpUtils::inRangeIPv4('192.168.1.1', '192.168.1.0/24');// true
IpUtils::inRangeIPv4('192.168.1.244', '192.168.1.0/24');// true
IpUtils::inRangeIPv4('192.168.1.255', '192.168.1.0/24');// true
IpUtils::inRangeIPv4('192.168.2.0', '192.168.1.0/24');// false

// Allowable:118.238.251.130/32
IpUtils::inRangeIPv4('118.238.251.129', '118.238.251.130/32');// false
IpUtils::inRangeIPv4('118.238.251.130', '118.238.251.130/32');// true
IpUtils::inRangeIPv4('118.238.251.131', '118.238.251.130/32');// false

// Allowable:118.238.251.130
IpUtils::inRangeIPv4('118.238.251.129', '118.238.251.130');// false
IpUtils::inRangeIPv4('118.238.251.130', '118.238.251.130');// true
IpUtils::inRangeIPv4('118.238.251.131', '118.238.251.130');// false
Parameters
$value : string

IP to be checked.

$allowable : string

IPv4 address or subnet in CIDR notation.

Return values
bool —

Whether the request IP matches the IP, or whether the request IP is within the CIDR subnet

inRangeIPv6()

Compares two IPv6 addresses. In case a subnet is given, it checks if it contains the request IP.

public static inRangeIPv6(string $value, string $allowable) : bool
use \X\Util\IpUtils;

IpUtils::inRangeIPv6('2404:7A81:B0A0:90FF:0000:0000:0000:0000', '2404:7A81:B0A0:9100::/64');// false
IpUtils::inRangeIPv6('2404:7A81:B0A0:9100:0000:0000:0000:0000', '2404:7A81:B0A0:9100::/64');// true
IpUtils::inRangeIPv6('2404:7A81:B0A0:9100:0000:0000:0000:0001', '2404:7A81:B0A0:9100::/64');// true
IpUtils::inRangeIPv6('2404:7A81:B0A0:9100:A888:5EE2:EA92:B618', '2404:7A81:B0A0:9100::/64');// true
IpUtils::inRangeIPv6('2404:7A81:B0A0:9100:D03:959E:7F47:9B77', '2404:7A81:B0A0:9100::/64');// true
IpUtils::inRangeIPv6('2404:7A81:B0A0:9100:FFFF:FFFF:FFFF:FFFE', '2404:7A81:B0A0:9100::/64');// true
IpUtils::inRangeIPv6('2404:7A81:B0A0:9100:FFFF:FFFF:FFFF:FFFF', '2404:7A81:B0A0:9100::/64');// true
IpUtils::inRangeIPv6('2404:7A81:B0A0:9101:0000:0000:0000:0000', '2404:7A81:B0A0:9100::/64');// false
Parameters
$value : string

IP to be checked.

$allowable : string

IPv6 address or subnet in CIDR notation.

Return values
bool —

Whether the IP is valid.

isIPv4()

Check if it is IPv4 format.

public static isIPv4(string $value) : bool
use \X\Util\IpUtils;

IpUtils::isIPv4('234.192.0.2');// true
IpUtils::isIPv4('234.198.51.100');// true
IpUtils::isIPv4('234.203.0.113');// true
IpUtils::isIPv4('0000:0000:0000:0000:0000:ffff:7f00:0001');// false
IpUtils::isIPv4('::1');// false
Parameters
$value : string

String to be checked.

Return values
bool —

IPv4 format or not.

isIPv6()

Check if it is IPv6 format.

public static isIPv6(string $value) : bool
use \X\Util\IpUtils;

IpUtils::isIPv6('234.192.0.2');// false
IpUtils::isIPv6('234.198.51.100');// false
IpUtils::isIPv6('234.203.0.113');// false
IpUtils::isIPv6('0000:0000:0000:0000:0000:ffff:7f00:0001');// true
IpUtils::isIPv6('::1');// true
Parameters
$value : string

String to be checked.

Return values
bool —

IPv6 format or not.


        
On this page

Search results