PHP Doku:: Konvertiert eine IP-Adresse im menschenlesbaren Format in eine komprimierte in_addr-Repräsentation - function.inet-pton.html

Verlauf / Chronik / History: (5) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzSonstige DiensteNetworkNetzwerk-Funktioneninet_pton

Ein Service von Reinhard Neidl - Webprogrammierung.

Netzwerk-Funktionen

<<inet_ntop

ip2long>>

inet_pton

(PHP 5 >= 5.1.0)

inet_ptonKonvertiert eine IP-Adresse im menschenlesbaren Format in eine komprimierte in_addr-Repräsentation

Beschreibung

string inet_pton ( string $address )

Die Funktion konvertiert eine menschenlesbare IPv4- oder IPv6-Adresse (sofern PHP mit IPv6-Support kompiliert wurde) in eine für die Adressfamilie geeignete 32-bittige oder 128-bittige Binärstruktur.

Parameter-Liste

address

Eine menschenlesbare IPv4- oder IPv6-Adresse.

Rückgabewerte

Gibt die in_addr-Representation der übergebenen address zurück.

Beispiele

Beispiel #1 inet_pton()-Beispiel

<?php
$in_addr 
inet_pton('127.0.0.1');

$in6_addr inet_pton('::1');
?>

Changelog

Version Beschreibung
5.3.0 Die Funktion steht jetzt auch auf Windowsplattformen zur Verfügung.

Siehe auch

  • ip2long() - Verwandelt eine gemäß IPv4-Protokoll angegebene Internet-Adresse vom Punkt-Format in die ausgeschriebene Adress-Angabe
  • long2ip() - Konvertiert eine (IPv4) Netzwerkadresse in einen String, der das Punkt-Format enthält ("Dotted-Format")
  • inet_ntop() - Konvertiert eine komprimierte Internetadresse in ein menschenlesbares Format


4 BenutzerBeiträge:
- Beiträge aktualisieren...
strata_ranger at hotmail dot com
13.09.2009 19:51
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);

?>
eric at vyncke org
18.07.2007 19:52
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
me at diogoresende dot net
16.05.2006 23: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.12.2005 9: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;
}
?>



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)