dismiss Step into the future! Click here to switch to the beta php.net site
downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

ip2long> <inet_ntop
[edit] Last updated: Fri, 28 Jun 2013

view this page in

inet_pton

(PHP 5 >= 5.1.0)

inet_ptonConverts a human readable IP address to its packed in_addr representation

Description

string inet_pton ( string $address )

This function converts a human readable IPv4 or IPv6 address (if PHP was built with IPv6 support enabled) into an address family appropriate 32bit or 128bit binary structure.

Parameters

address

A human readable IPv4 or IPv6 address.

Return Values

Returns the in_addr representation of the given address, or FALSE if a syntactically invalid address is given (for example, an IPv4 address without dots or an IPv6 address without colons).

Examples

Example #1 inet_pton() Example

<?php
$in_addr 
inet_pton('127.0.0.1');
 
$in6_addr inet_pton('::1');
?>

Changelog

Version Description
5.3.0 This function is now available on Windows platforms.

See Also

  • ip2long() - Converts a string containing an (IPv4) Internet Protocol dotted address into a proper address
  • long2ip() - Converts an (IPv4) Internet network address into a string in Internet standard dotted format
  • inet_ntop() - Converts a packed internet address to a human readable representation



ip2long> <inet_ntop
[edit] Last updated: Fri, 28 Jun 2013
 
add a note add a note User Contributed Notes inet_pton - [6 notes]
up
1
me at diogoresende dot net
7 years ago
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.
up
0
eric at vyncke org
5 years ago
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
up
0
francis dot besset at gmail dot com
1 year ago
It is possible to verify if PHP was compiled with --disable-ipv6 option by AF_INET6 constant.

<?php

if (defined('AF_INET6')) {
  echo
"PHP was compiled without --disable-ipv6 option";
} else {
  echo
"PHP was compiled with --disable-ipv6 option";
}

?>
up
0
strata_ranger at hotmail dot com
3 years ago
If the input string is not a readable IP address, inet_pton() generates an E_WARNING and returns FALSE.  The same is true for inet_ntop().

Also, inet_pton() does not recognize netmask notation (e.g: "1.2.3.4/24" or "1:2::3:4/64") in the input string.  This differs from how some database systems (like postgreSQL) support IP address types, so if you need that sort of functionality when processing IP addresses in PHP you'll have to write it in yourself.

A rough example:

<?php

// Sample IP addresses
$ipaddr = '1.2.3.4/24'; // IPv4 with /24 netmask
$ipaddr = '1:2::3:4/64'; // IPv6 with /64 netmask

// Strip out the netmask, if there is one.
$cx = strpos($ipaddr, '/');
if (
$cx)
{
 
$subnet = (int)(substr($ipaddr, $cx+1));
 
$ipaddr = substr($ipaddr, 0, $cx);
}
else
$subnet = null; // No netmask present

// Convert address to packed format
$addr = inet_pton($ipaddr);

// Let's display it as hexadecimal format
foreach(str_split($addr) as $char) echo str_pad(dechex(ord($char)), 2, '0', STR_PAD_LEFT);
echo
"<br />\n";

// Convert the netmask
if (is_integer($subnet))
{
 
// Maximum netmask length = same as packed address
 
$len = 8*strlen($addr);
  if (
$subnet > $len) $subnet = $len;
 
 
// Create a hex expression of the subnet mask
 
$mask  = str_repeat('f', $subnet>>2);
  switch(
$subnet & 3)
  {
  case
3: $mask .= 'e'; break;
  case
2: $mask .= 'c'; break;
  case
1: $mask .= '8'; break;
  }
 
$mask = str_pad($mask, $len>>2, '0');

 
// Packed representation of netmask
 
$mask = pack('H*', $mask);
}

// Display the netmask as hexadecimal
foreach(str_split($mask) as $char) echo str_pad(dechex(ord($char)), 2, '0', STR_PAD_LEFT);

?>
up
-1
djmaze(AT)dragonflycms(.)org
7 years ago
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;
}
?>
up
-2
SK at example dot com
10 months ago
I noticed that inet_pton() doesn't like compressed single hex groups
i.e. ::ffff:ffff:ffff:ffff:ffff:ffff:ffff and ::ffff:ffff:ffff:ffff:ffff:127.0.0.1

work around and/or PHP 4.0.1+ add-in:
<?php
function ip2bin($ip)
{
   
$s = $ipv4 = '';

    if(
strpos($ip, ':') !== false)
    {
        if(!(
$l = preg_match_all('#(?|::|(?|\d{1,3}\.){3}\d{1,3}|[0-9a-f]{1,4})#i', $ip, $m)) || $l > 8)
            return
false;
        foreach(
$m[0] as $p)
        {
            if(
$p == '::')
               
$s .= str_repeat('0000', 8 - $l + (strpos($m[0][$l-1], '.') === false));
            else if(
strpos($p, '.') !== false)
               
$ipv4 = ip2bin($p);
            else
               
$s .= str_pad($p, 4, '0', STR_PAD_LEFT);
        }
        if(
$ipv4 === false)
            return
false;
        return
pack('H*', $s). $ipv4;
    }
    else if(
strpos($ip, '.') !== false)
    {
       
$x = explode('.', $ip);

        if(
count($x) != 4)
            return
false;
        foreach(
$x as $i)
           
$s .= chr((int) $i);
        return
$s;
    }
    return
false;
}
var_dump(
    @
inet_pton('::ffff:ffff:ffff:ffff:ffff:ffff:ffff'), // bool(false)
   
ip2bin('::ffff:ffff:ffff:ffff:ffff:ffff:ffff'),
    @
inet_pton('::ffff:ffff:ffff:ffff:ffff:127.0.0.1'), // bool(false)
   
ip2bin('::ffff:ffff:ffff:ffff:ffff:127.0.0.1')
);
?>

 
show source | credits | stats | sitemap | contact | advertising | mirror sites