ipUtil
in package
Utility to check IPv4 or IPv6 address to a list of addresses or networks (using CIDR)
Parts derived from Cloudflare https://github.com/cloudflare/Cloudflare-WordPress/ Symphony https://github.com/symfony/http-foundation/blob/7.1/IpUtils.php
Tags
Table of Contents
- HTTP_IP_HEADERS = ['HTTP_X_REAL_IP', 'HTTP_CF_CONNECTING_IP', 'HTTP_AKAMAI_ORIGIN_HOP', 'HTTP_FASTLY_CLIENT_IP', 'HTTP_INCAP_CLIENT_IP', 'HTTP_TRUE_CLIENT_IP', 'HTTP_X_IP_TRAIL', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_FORWARDED', 'HTTP_CLIENT_IP', 'REMOTE_ADDR']
- PRIVATE_SUBNETS = [ '127.0.0.0/8', // RFC1700 (Loopback) '10.0.0.0/8', // RFC1918 '192.168.0.0/16', // RFC1918 '172.16.0.0/12', // RFC1918 '169.254.0.0/16', // RFC3927 '0.0.0.0/8', // RFC5735 '240.0.0.0/4', // RFC1112 '::1/128', // Loopback 'fc00::/7', // Unique Local Address 'fe80::/10', // Link Local Address '::ffff:0:0/96', // IPv4 translations '::/128', ]
- checkIp() : bool
- Checks if an IPv4 or IPv6 address is contained in the list of given IPs or subnets.
- checkIp4() : bool
- Compares two IPv4 addresses.
- checkIp6() : bool
- Compares two IPv6 addresses.
- getRemoteIP() : string
- Get the request IP address
- isIpv4() : bool
- Checks if the ip is v4.
- isIpv6() : bool
- Checks if the ip is v6.
- isPrivateIp() : bool
- Checks if an IPv4 or IPv6 address is contained in the list of private IP subnets.
Constants
HTTP_IP_HEADERS
public
array<string|int, mixed>
HTTP_IP_HEADERS
= ['HTTP_X_REAL_IP', 'HTTP_CF_CONNECTING_IP', 'HTTP_AKAMAI_ORIGIN_HOP', 'HTTP_FASTLY_CLIENT_IP', 'HTTP_INCAP_CLIENT_IP', 'HTTP_TRUE_CLIENT_IP', 'HTTP_X_IP_TRAIL', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_FORWARDED', 'HTTP_CLIENT_IP', 'REMOTE_ADDR']
http headers containing remote IP address
PRIVATE_SUBNETS
public
array<string|int, mixed>
PRIVATE_SUBNETS
= [
'127.0.0.0/8',
// RFC1700 (Loopback)
'10.0.0.0/8',
// RFC1918
'192.168.0.0/16',
// RFC1918
'172.16.0.0/12',
// RFC1918
'169.254.0.0/16',
// RFC3927
'0.0.0.0/8',
// RFC5735
'240.0.0.0/4',
// RFC1112
'::1/128',
// Loopback
'fc00::/7',
// Unique Local Address
'fe80::/10',
// Link Local Address
'::ffff:0:0/96',
// IPv4 translations
'::/128',
]
private subnets
Methods
checkIp()
Checks if an IPv4 or IPv6 address is contained in the list of given IPs or subnets.
public
static checkIp(string $requestIp, string|array<string|int, mixed> $ipList) : bool
Parameters
- $requestIp : string
-
IP to check
- $ipList : string|array<string|int, mixed>
-
List of IPs or subnets (can be a string if only a single one)
Return values
bool —Whether the IP is valid
checkIp4()
Compares two IPv4 addresses.
public
static checkIp4(string $requestIp, string $ip) : bool
In case a subnet is given, it checks if it contains the request IP.
Parameters
- $requestIp : string
-
IPv4 address to check
- $ip : 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.
checkIp6()
Compares two IPv6 addresses.
public
static checkIp6(string $requestIp, string $ip) : bool
In case a subnet is given, it checks if it contains the request IP.
author David Soria Parra
Parameters
- $requestIp : string
-
IPv6 address to check
- $ip : string
-
IPv6 address or subnet in CIDR notation
Tags
Return values
bool —Whether the IP is valid
getRemoteIP()
Get the request IP address
public
static getRemoteIP() : string
Return values
string —IP address or null
isIpv4()
Checks if the ip is v4.
public
static isIpv4(string $ip[, bool $validate = false ]) : bool
Parameters
- $ip : string
-
IP to check
- $validate : bool = false
-
validate with filter_var
Return values
bool —return true if ipv4
isIpv6()
Checks if the ip is v6.
public
static isIpv6(string $ip[, bool $validate = false ]) : bool
Parameters
- $ip : string
-
IP to check
- $validate : bool = false
-
validate with filter_var
Return values
bool —return true if ipv6
isPrivateIp()
Checks if an IPv4 or IPv6 address is contained in the list of private IP subnets.
public
static isPrivateIp(string $requestIp) : bool
Parameters
- $requestIp : string