Not so easy in the function below... It is not handling the case of '::' which can happen in an IPv6 and represents any number of 0, addresses could be as simple as ff05::1
inet_pton
(PHP 5 >= 5.1.0)
inet_pton — Convertit une adresse IP lisible en sa représentation in_addr
Description
string inet_pton
( string $address
)
Convertit une adresse IPv4 ou IPv6 (si PHP a été compilé avec le support IPv6) humainement lisible en une structure binaire appropriée de famille d'adresses 32bit ou 128bit.
Liste de paramètres
- address
-
Une adresse IPv4 ou IPv6.
Valeurs de retour
Retourne la représentation in_addr de l'adresse fournie par le paramètre address
Exemples
Exemple #1 Exemple avec inet_pton()
<?php
$in_addr = inet_pton('127.0.0.1');
$in6_addr = inet_pton('::1');
?>
Notes
Historique
| Version | Description |
|---|---|
| 5.3.0 | Cette fonction est maintenant disponible sur les plateformes Windows. |
inet_pton
eric at vyncke org
18-Jul-2007 07:52
18-Jul-2007 07:52
me at diogoresende dot net
16-May-2006 11:34
16-May-2006 11:34
If you want to use the above function you should test for ':' character before '.'. Meaning, you should check if it's an ipv6 address before checking for ipv4.
Why? IPv6 allows this type of notation:
::127.0.0.1
If you check for '.' character you will think this is an ipv4 address and it will fail.
djmaze(AT)dragonflycms(.)org
14-Dec-2005 09:01
14-Dec-2005 09:01
If you need the functionality but your PHP version doesn't have the functionality (like on windows) the following might help
<?php
function inet_pton($ip)
{
# ipv4
if (strpos($ip, '.') !== FALSE) {
$ip = pack('N',ip2long($ip));
}
# ipv6
elseif (strpos($ip, ':') !== FALSE) {
$ip = explode(':', $ip);
$res = str_pad('', (4*(8-count($ip))), '0000', STR_PAD_LEFT);
foreach ($ip as $seg) {
$res .= str_pad($seg, 4, '0', STR_PAD_LEFT);
}
$ip = pack('H'.strlen($res), $res);
}
return $ip;
}
?>
