PHP Code Snippet - Validate IPv4, IPv6 IP Addresses
These PHP Code snippets show how to validate IPv4 or IPv6 IP addresses. Also it allows checking IP address belongs to private network address spaces.
Bookmark:
Validate IPv4, IPv6 IP Addresses
This PHP code snippets validate IPv4, IPv6 IP addresses. First method uses regular expression to validate IPv4 IP address and other methods uses PHP built in function filter_var to validate IPv4 and IPv6 IP addresses.
Validate IPv4 IP address with regular expression
/** * Validate an IPv4 IP address * * @param string $ip * @return boolean - true/false */ function isValidIP($ip) { return preg_match('/^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/',$ip); }
filter_var function can validate if an IP address is valid using FILTER_VALIDATE_IP validate filter.
Using this validate filter can validates value as IP address, optionally only IPv4 or IPv6 or not from private or reserved ranges.
Note that the filter_var() function requires at least PHP version 5.2.0.
Validating an IPv4 IP address
/** * Validate an IPv4 IP address * * @param string $ip * @return boolean - true/false */ function isValidIP($ip) { if ( false === filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) ) { return false; } else { return true; } }
Validating an IPv4 IP address, excluding private range addresses
/** * Validate an IPv4 IP address * excluding private range addresses * * @param string $ip * @return boolean - true/false */ function isValidIPNoPriv($ip) { if ( false === filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE) ) { return false; } else { return true; } }
Validating an IPv6 IP address
/** * Validate an IPv6 IP address * * @param string $ip * @return boolean - true/false */ function isValidIPv6($ip) { if ( false === filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) ) { return false; } else { return true; } }
Validating an IPv6 IP address, excluding private range addresses
/** * Validate an IPv6 IP address * excluding private range addresses * * @param string $ip * @return boolean - true/false */ function isValidIPv6NoPriv($ip) { if ( false === filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6 | FILTER_FLAG_NO_PRIV_RANGE) ) { return false; } else { return true; } }
Private IPv4 address spaces
IPv4 address ranges for private networks
RFC1918 name | IP address range | number of addresses | classful description | largest CIDR block (subnet mask) | host id size |
---|---|---|---|---|---|
24-bit block | 10.0.0.0 – 10.255.255.255 | 16,777,216 | single class A | 10.0.0.0/8 (255.0.0.0) | 24 bits |
20-bit block | 172.16.0.0 – 172.31.255.255 | 1,048,576 | 16 contiguous class Bs | 172.16.0.0/12 (255.240.0.0) | 20 bits |
16-bit block | 192.168.0.0 – 192.168.255.255 | 65,536 | 256 contiguous class Cs | 192.168.0.0/16 (255.255.0.0) | 16 bits |
Private IPv6 address spaces
The address block fc00::/7 has been reserved. These addresses are called Unique Local Addresses (ULA). They are defined as being unicast in character and contain a 40-bit random number in the routing prefix to prevent collisions when two private networks are interconnected.
PHP Keywords Used:
- preg_match
- filter_var
Code Snippet Information:
- Applies To: Validate IPv4, IPv6 IP Addresses
- Programming Language : PHP
External Resources: