PHP 5.6.0beta1 released


(PHP 5 >= 5.1.0)

inet_ptonİnsan okuyabilir bir IP adresini in_addr gösterimine dönüştürür


string inet_pton ( string $ip_adresi )

Bu işlev, bir insan okuyabilir IPv4 veya (PHP, IPv6 desteğiyle derlenmişse) IPv6 adresini 32 veya 128 bitlik ikil yapılı bir adres ailesine dönüştürür.



Bir insan okuyabilir IPv4 veya IPv6 adresi.

Dönen Değerler

Belirtilen ip_adresi'nin in_addr gösterimiyle döner.


Örnek 1 - inet_pton() örneği


$in6_addr inet_pton('::1');

Sürüm Bilgisi

Sürüm: Açıklama
5.3.0 İşlev Windows platformlarında da kullanılabilir hale geldi.

Ayrıca Bakınız

  • ip2long() - Noktalı IP adresi gösterimini içeren bir dizgeyi sayısal adrese dönüştürür
  • long2ip() - Bir (IPv4) sayısal ağ adresini noktalı gösterimi içeren bir dizgeye dönüştürür
  • inet_ntop() - Bir IP adresinin in_addr gösterimini insan okuyabilir gösterime dönüştürür

add a note add a note

User Contributed Notes 4 notes

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:


If you check for '.' character you will think this is an ipv4 address and it will fail.
strata_ranger at hotmail dot com
4 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: "" 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:


// Sample IP addresses
$ipaddr = ''; // 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 (
$subnet = (int)(substr($ipaddr, $cx+1));
$ipaddr = substr($ipaddr, 0, $cx);
$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);
"<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);
$subnet & 3)
3: $mask .= 'e'; break;
2: $mask .= 'c'; break;
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);

francis dot besset at gmail dot com
2 years ago
It is possible to verify if PHP was compiled with --disable-ipv6 option by AF_INET6 constant.


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

eric at vyncke org
6 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