Validate IPv6 Address

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Can anybody know how to validate IPV4 and IPV6 address in PHP. I am
using PHP 4.2.3 for some compatiability reason.

Re: Validate IPv6 Address wrote:
Quoted text here. Click to load it

// Regex by Daniel Adam
$pattern[ipv4] =  

// Checks whether a given IPv6 address string could be valid based on  
full and compressed IPv6 addresses as defined in RFC 2373 -
// Regex by Jeff Johnson
$pattern[ipv6] =  

Pass the value to be tested to the below as $value:

$valid_ipv4 = preg_match( $pattern[ipv4], $value ) ? TRUE : FALSE;

$valid_ipv6 = preg_match( $pattern[ipv6], $value ) ? TRUE : FALSE;

Good luck,

Re: Validate IPv6 Address

why not combine the two ip functions ip2long and long2ip:


  // make sure IPs are valid. also converts a non-complete IP into
  // a proper dotted quad as explained below.
  $ip = long2ip(ip2long("")); // ""
  $ip = long2ip(ip2long("10.0.0")); // ""
  $ip = long2ip(ip2long("10.0.256")); // ""

Site Timeline