Validate IPv4, IPv6 IP Addresses

PHP Code Snippet - Validate IPv4, IPv6 IP Addresses

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 nameIP address rangenumber of addressesclassful descriptionlargest CIDR block (subnet mask)host id size
24-bit block10.0.0.0 – 10.255.255.25516,777,216single class A10.0.0.0/8 (255.0.0.0)24 bits
20-bit block172.16.0.0 – 172.31.255.2551,048,57616 contiguous class Bs172.16.0.0/12 (255.240.0.0)20 bits
16-bit block192.168.0.0 – 192.168.255.25565,536256 contiguous class Cs192.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:

Leave a comment