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', // CloudFare '1.1.1.1', '1.0.0.1', ]
- IP addresses of some well known public DNS servers.
Properties
- $host : string
- $ip : string
- $backcompat : array<string|int, mixed>
Methods
- __construct() : mixed
- Constructor.
- __toString() : string
- Returns the string form of the IP address.
- create() : object
- Convenience wrapper for constuctor.
- expand() : string
- Returns the fully expanded string form of the IP address.
- expandIPv6() : string|false
- Backward compatibility wrapper for the expand() method.
- 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.
- hostFromIp() : string
- Backward compatibility wrapper for the getHost() method.
- inet_dtop() : string
- Backward compatibility wrapper for the __toString() method.
- inet_ptod() : string
- Backward compatibility wrapper for the toBinary() method.
- 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.
- isValidIP() : string
- Backward compatibility wrapper for the isValid() method.
- isValidIPv6() : string
- Another backward compatibility wrapper for the isValid() method.
- range2ip() : string
- Convert a range of IP addresses into a single string.
- toBinary() : string
- 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',
// CloudFare
'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.
$backcompat
private
static array<string|int, mixed>
$backcompat
= ['func_names' => ['ip2range' => 'ip2range', 'range2ip' => 'range2ip', 'isValidIP' => 'isValidIP', 'isValidIPv6' => 'isValidIPv6', 'hostFromIp' => 'host_from_ip', 'inet_ptod' => 'inet_ptod', 'inet_dtop' => 'inet_dtop', 'expandIPv6' => 'expandIPv6']]
BackwardCompatibility settings for this class.
Methods
__construct()
Constructor.
public
__construct(string|null $ip) : mixed
If the passed string is not a valid IP address, it will be set to ''.
Parameters
- $ip : string|null
-
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 constuctor.
public
static create(string $ip) : object
This is just syntactical sugar to ease method chaining.
Parameters
- $ip : string
-
The IP address in either string or binary form.
Return values
object —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.
expandIPv6()
Backward compatibility wrapper for the expand() method.
public
static expandIPv6(string $ip[, bool $return_bool_if_invalid = true ]) : string|false
Parameters
- $ip : string
-
An IPv6 address.
- $return_bool_if_invalid : bool = true
-
Controls return type if address is invalid. True for boolean, false for empty string. Default: true.
Return values
string|false —The expanded IPv6 address, or false/an empty string if address was invalid.
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']. Likewise, a class's static methods are not exported as global functions unless explicitly included in $backcompat['func_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.
$backcompat['func_names'] is a simple array where the keys are the names of one or more of a class's static methods, and the values are the names that should be used for global functions that will encapsulate those methods. Methods that are not named in this array will not be exported.
Adding non-static properties or methods 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.
hostFromIp()
Backward compatibility wrapper for the getHost() method.
public
static hostFromIp(string $ip) : string
Parameters
- $ip : string
-
An IP address in either string or binary form.
Return values
string —The host name.
inet_dtop()
Backward compatibility wrapper for the __toString() method.
public
static inet_dtop(string $ip) : string
Parameters
- $ip : string
-
An IP address in either string or binary form.
Return values
string —The host name.
inet_ptod()
Backward compatibility wrapper for the toBinary() method.
public
static inet_ptod(string $ip) : string
Parameters
- $ip : string
-
An IP address in either string or binary form.
Return values
string —The host name.
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 maxium 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
boolisValidIP()
Backward compatibility wrapper for the isValid() method.
public
static isValidIP(string $ip) : string
Parameters
- $ip : string
-
An IP address in either string or binary form.
Return values
string —Whether $ip is a valid IP address.
isValidIPv6()
Another backward compatibility wrapper for the isValid() method.
public
static isValidIPv6(string $ip) : string
This one checks specifically for valid IPv6 addresses.
Parameters
- $ip : string
-
An IPv6 address in either string or binary form.
Return values
string —Whether $ip is a valid IPv6 address.
range2ip()
Convert a range of IP addresses into a single string.
public
static range2ip(string $low, string $high) : string
It's practically the reverse function of ip2range().
Parameters
- $low : string
-
The low end of the range.
- $high : string
-
The high end of the range.
Return values
string —A string indicating the range.
toBinary()
Returns the binary form of the IP address.
public
toBinary() : string
Return values
stringtoHex()
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.