473,799 Members | 3,310 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Validate IPv6 Address

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

Feb 7 '06 #1
2 5820
pr*********@gma il.com wrote:
Can anybody know how to validate IPV4 and IPV6 address in PHP. I am
using PHP 4.2.3 for some compatiability reason.

// Regex by Daniel Adam
// http://www.regxlib.com/REDetails.aspx?regexp_id=1139
$pattern[ipv4] =
'/^((\d|[1-9]\d|2[0-4]\d|25[0-5]|1\d\d)(?:\.(\d |[1-9]\d|2[0-4]\d|25[0-5]|1\d\d)){3})$/';

// Checks whether a given IPv6 address string could be valid based on
full and compressed IPv6 addresses as defined in RFC 2373 -
http://www.faqs.org/rfcs/rfc2373.html
// Regex by Jeff Johnson
// http://www.regxlib.com/REDetails.aspx?regexp_id=1000
$pattern[ipv6] =
'/^((([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}:[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)?[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){4}:([0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){3}:([0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){2}:([0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}((\b( (25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\ b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\ b))|(([0-9A-Fa-f]{1,4}:){0,5}:(( \b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\ b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\ b))|(::([0-9A-Fa-f]{1,4}:){0,5}((\ b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\ b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\ b))|([0-9A-Fa-f]{1,4}::([0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})|(::([0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){1,7}:)) $/';
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,
Juliette
Feb 7 '06 #2
why not combine the two ip functions ip2long and long2ip:

from http://us2.php.net/manual/en/function.ip2long.php

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

Feb 8 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
5956
by: Torsten Schmidt | last post by:
Hi, I'm trying to connect to a mysql-Server using PHP's mysql-function mysql_connect. The host on which the mysql-server is running is not the same as the host apache and php are running on. The two computers are connected in an IPv6-network. No matter if I give the hostname (defined in /etc/hosts), the IPv6-address ("fec0::4") or the IPv6-address in square brackets ("") the function mysql_connect always prints "Unknown MYSQL-Server "....
8
2549
by: John Burton | last post by:
I'm not sure if this is a question about python socket support or sockets in general ... I have a socket server with which I want to accept incoming connections on ipv4 and ipv6. I've found that if I create the listening socket with sock = socket(AF_INET, SOCK_STREAM) it will accept connections on ipv4 (only) and if I create it with sock = socket(AF_INET6, SOCK_STREAM) it will accept connections with ipv6 (only)
3
5872
by: Frank Jiao | last post by:
I can get my local IPv6 address in Win2003 use the source code as below: string localName = Dns.GetHostName(); string address = ""; string scopeId = ""; IPHostEntry hostEntry = Dns.Resolve( localName ); foreach (IPAddress curAdd in hostEntry.AddressList) { if(curAdd.AddressFamily.ToString() ==
2
6398
by: PaulH | last post by:
I am attempting to write a functon that can perform IPv6 compliant pings. But, Icmp6SendEcho2 causes an access violation whenever it is called: First-chance exception at 0x76d641e8 in mping2.exe: 0xC0000005: Access violation reading location 0x00000000. Unhandled exception at 0x76d641e8 in mping2.exe: 0xC0000005: Access violation reading location 0x00000000. I have been unable to pinpoint the cause of the access violation so far, so I...
0
1420
by: Addam | last post by:
Anyone, I am writting a server that accepts a list of IPv4 and IPv6 address. The IPv4 address are padded with leading zeros. I belive this allows me to just point to the beging and run both IPv4 addresses and IPv6 address though the same conversion function. However, i am having a hard time finding a simple way to this. I do not want to communicate with the addresses so i would like to avoid setting up anything extra. So does anyone...
8
9943
by: prabhuram.k | last post by:
Can anybody know how to validate IPV4 and IPV6 address in C++
3
6248
by: jiri.juranek | last post by:
Hello, is there any common function for validation if string contains valid ip address(both ipv4 and ipv6)? Or does sb wrote some regular expression for this? thanks J
2
9433
by: Valerie Hough | last post by:
My app has so far only encountered IPv4 addresses and I use: Dns.GetHostByName( "someOtherComputer", portNumber ).AddressList. Can someone please point me to an example of how to turn this into an IPv6 address? Thanks in advance. Chris Hough
8
8951
by: Giampaolo Rodola' | last post by:
I'm not sure if this is a question about python programming, system administration or sockets in general... I have the FTP server in my signature to which I'd want to add IPv6 support. My hosting company provides me a common IPv4 address. I'd like to set up my workstation (Windows XP or Linux Debian, doesn't really matter) to temporarily use IPv6 for trying to add such feature to my library (I work on both Windows XP and Linux). Could...
14
9300
by: Simon | last post by:
Hi, is there a straight forward way of converting IPv4 to IPv6? I thought that it was just a matter of converting 32 bits to 128 bits, (by adding 96 leading 0s), but that does not seem right in some/most cases. For example, 127.0.0.1, (IPv4 localhost), does not convert ::1, (IPv6 localhost)
0
9541
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10485
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10252
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9073
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7565
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4141
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.