IP
in package
implements
Stringable
uses
BackwardCompatibility
Represents an IP address and allows performing various operations on it.
Table of Contents
Interfaces
- Stringable
Constants
- DNS_SERVERS = [ // Google '8.8.8.8', '8.8.4.4', // OpenDNS '208.67.222.222', '208.67.220.220', // CloudFlare '1.1.1.1', '1.0.0.1', ]
- IP addresses of some well known public DNS servers.
Properties
Methods
- __construct() : mixed
- Constructor.
- __toString() : string
- Returns the string form of the IP address.
- create() : self
- Convenience wrapper for constructor.
- expand() : string
- Returns the fully expanded string form of the IP address.
- exportStatic() : void
- Provides a way to export a class's public static properties and methods to global namespace.
- getHost() : string
- Tries to finds the name of the host that corresponds to this IP address.
- ip2range() : array<string|int, mixed>
- Given an IP address or range of IP addresses, possibly containing wildcards, returns an array containing two IP addresses that bound the possible range.
- isValid() : bool
- Check whether this is a valid IP address.
- matchToCIDR() : bool
- Detect if a IP is in a CIDR address.
- range2ip() : string
- Convert a range of IP addresses into a single string.
- toBinary() : string|bool
- Returns the binary form of the IP address.
- toHex() : string
- Returns the hexadecimal form of the IP address.
- getHostByAddr() : string
- Like PHP's gethostbyaddr(), but with a timeout.
- getReverseDnsQuery() : string
- Gets the raw data to send in a reverse DNS lookup query for this address.
- hostLookup() : string
- Asks the operating system to look up the host name for this IP address.
Constants
DNS_SERVERS
IP addresses of some well known public DNS servers.
public
mixed
DNS_SERVERS
= [
// Google
'8.8.8.8',
'8.8.4.4',
// OpenDNS
'208.67.222.222',
'208.67.220.220',
// CloudFlare
'1.1.1.1',
'1.0.0.1',
]
Properties
$host
protected
string
$host
The host name associated with this IP address.
$ip
protected
string
$ip
The string representation of the IP address.
Methods
__construct()
Constructor.
public
__construct(string|null|self $ip) : mixed
If the passed string is not a valid IP address, it will be set to ''.
Parameters
- $ip : string|null|self
-
The IP address in either string or binary form.
__toString()
Returns the string form of the IP address.
public
__toString() : string
Return values
stringcreate()
Convenience wrapper for constructor.
public
static create(string $ip) : self
This is just syntactical sugar to ease method chaining.
Parameters
- $ip : string
-
The IP address in either string or binary form.
Return values
self —The created object.
expand()
Returns the fully expanded string form of the IP address.
public
expand() : string
For IPv4, this just returns the standard string. For IPv6, this returns the fully expanded form of the string.
Return values
string —The fully expanded form of the IP address.
exportStatic()
Provides a way to export a class's public static properties and methods to global namespace.
public
static exportStatic() : void
To do so:
- Use this trait in the class.
- At the END of the class's file, call its exportStatic() method.
Although it might not seem that way at first glance, this approach conforms to section 2.3 of PSR 1, since executing this method is simply a dynamic means of declaring functions when the file is included; it has no other side effects.
Regarding the $backcompat items:
A class's static properties are not exported to global variables unless explicitly included in $backcompat['prop_names'].
$backcompat['prop_names'] is a simple array where the keys are the names of one or more of a class's static properties, and the values are the names of global variables. In each case, the global variable will be set to a reference to the static property. Static properties that are not named in this array will not be exported.
Adding non-static properties to the $backcompat arrays will produce runtime errors. It is the responsibility of the developer to make sure not to do this.
getHost()
Tries to finds the name of the host that corresponds to this IP address.
public
getHost([int $timeout = 1000 ]) : string
Parameters
- $timeout : int = 1000
-
Milliseconds until timeout. Set to 0 for no timeout. Default: 1000.
Return values
string —The host name, or the IP address string on failure.
ip2range()
Given an IP address or range of IP addresses, possibly containing wildcards, returns an array containing two IP addresses that bound the possible range.
public
static ip2range(string $addr) : array<string|int, mixed>
Used to convert a user-readable format to a format suitable for the database.
-
If $addr is one complete IP address, the return value will contain two copies of that address, since the range starts and ends with the same value.
Examples:
'1.2.3.4' -> array('low' => '1.2.3.4', 'high' => '1.2.3.4')
-
If $addr is one IP address with wildcards, the return value will contain the minimum and maximum values possible with those wildcards.
Examples:
'1.2.' -> array('low' => 1.2.0.0', 'high' => '1.2.255.255') '1..*.4' -> array('low' => '1.0.0.4', 'high' => '1.255.255.4')
-
If $addr contains two complete IP addresses, the return value will contain those two addresses.
Examples:
'1.2.3.4-5.6.7.8' -> array('low' => '1.2.3.4', 'high' => '5.6.7.8')
-
If $addr contains two IP addresses, either of which contain wildcards, then any wildcards in the first one will be made as low as possible, and any wildcards in the second one will be made as high as possible.
Examples:
'1.2.-5.6.7.8' -> array('low' => '1.2.0.0', 'high' => '5.6.7.8') '1.2.3.-5.6.7.8' -> array('low' => '1.2.3.0', 'high' => '5.6.7.8') '1.2..4-5.6.' -> array('low' => '1.2.0.4', 'high' => '5.6.255.255')
The examples only show IPv4, but this also works for IPv6 addresses.
Parameters
- $addr : string
-
The full IP
Return values
array<string|int, mixed> —An array containing two instances of this class.
isValid()
Check whether this is a valid IP address.
public
isValid([int $flags = 0 ]) : bool
Parameters
- $flags : int = 0
-
Optional flags for filter_var's third parameter.
Return values
boolmatchToCIDR()
Detect if a IP is in a CIDR address.
public
matchToCIDR(string $cidr_address) : bool
Parameters
- $cidr_address : string
-
CIDR address to verify.
Return values
bool —Whether the IP matches the CIDR.
range2ip()
Convert a range of IP addresses into a single string.
public
static range2ip(string|IP $low, string|IP $high) : string
It's practically the reverse function of ip2range().
Parameters
Return values
string —A string indicating the range.
toBinary()
Returns the binary form of the IP address.
public
toBinary() : string|bool
Return values
string|booltoHex()
Returns the hexadecimal form of the IP address.
public
toHex() : string
Return values
stringgetHostByAddr()
Like PHP's gethostbyaddr(), but with a timeout.
protected
getHostByAddr([int $timeout = 1000 ]) : string
Inspired by code by king dot macro at gmail dot com. https://www.php.net/manual/en/function.gethostbyaddr.php#46869
Parameters
- $timeout : int = 1000
-
Milliseconds until timeout. Default: 1000.
Return values
string —The host name, or the IP address on failure.
getReverseDnsQuery()
Gets the raw data to send in a reverse DNS lookup query for this address.
protected
getReverseDnsQuery() : string
Return values
string —The raw query to send.
hostLookup()
Asks the operating system to look up the host name for this IP address.
protected
hostLookup([int $timeout = 1000 ]) : string
Only works if we have shell_exec permission and if the host, nslookup, and/or dscacheutil commands are available.
Parameters
- $timeout : int = 1000
Return values
string —The host name, or an empty string on failure.