inet_pton

(PHP 5 >= 5.1.0, PHP 7)

inet_pton人間が読める形式の IP アドレスを、パックされた in_addr 形式に変換する

説明

string inet_pton ( string $address )

この関数は、人間が読める形式の IPv4 あるいは IPv6 (PHP が IPv6 サポートを有効にしてビルドされている場合) のアドレスを 32 ビットあるいは 128 ビットのバイナリ形式に変換します。

パラメータ

address

可読形式の IPv4 アドレスあるいは IPv6 アドレス。

返り値

指定した addressin_addr 形式で表したものを返します。 構文的に無効な address (ドットのない IPv4 アドレスやコロンのない IPv6 アドレスなど) を指定した場合は FALSE を返します。

例1 inet_pton() の例

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

変更履歴

バージョン 説明
5.3.0 この関数は Windows プラットフォームでも使用可能となりました。

参考

  • ip2long() - ドット表記の (IPv4) IP アドレスを、長整数表現に変換する
  • long2ip() - 長整数評言のインターネットアドレスを (IPv4) インターネット標準ドット表記に変換する
  • inet_ntop() - パックされたインターネットアドレスを、人間が読める形式に変換する

add a note add a note

User Contributed Notes 7 notes

up
7
strata_ranger at hotmail dot com
8 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
3
francis dot besset at gmail dot com
6 years 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
4
TuRn3r
3 years ago
Be careful, address with leading 0 return false.

Example :
<?php
inet_pton
('172.27.1.04'); // return false
inet_pton('172.27.1.4') ;// return the good result
?>
up
3
me at diogoresende dot net
11 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
1
dave at php dot net
2 years ago
If you are receiving an "Unrecognized address" error for an IPv6 address, it's possible your version of PHP has not been compiled with IPv6 support.

To check, load up phpinfo(); and look to see if "IPv6 Support" is set to "disabled".
up
-1
tobias
1 year ago
Oh my god:

0000:0000:0000:0000:0000:0000:0127:2258
becomes '"X.

Do not forget to escape it
up
-8
eric at vyncke org
10 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
To Top