PHP Network inet_pton() Function



The PHP Network inet_pton() function is used to convert a human-readable IP address to binary format. This function works with both IPv4 and IPv6 addresses, as long as your PHP configuration supports IPv6. An IP address is a unique integer that identifies a device on a network and this function converts it to a system-compatible format.

Simply provide an IP address in readable form and the function will return a similarly packed binary structure. If the address is incorrect or improperly formatted, it returns false. This is useful for network tasks that require an IP address in a specified binary format.

Syntax

Below is the syntax of the PHP Network inet_pton() function −

string inet_pton ( string $ip )

Parameters

This function accepts $ip parameter which is a human readable IPv4 or IPv6 address.

Return Value

The inet_pton() function returns the in_addr representation of the given IP address, or false if it is syntactically prohibited.

PHP Version

First introduced in core PHP 5.1.0, the inet_pton() function continues to function easily in PHP 7, and PHP 8.

Example 1

In this simple implementation, we will convert an IPv4 address into its binary form using the PHP Network inet_pton() function. The binary result is then displayed in hexadecimal format with the help of bin2hex().

<?php
   // Convert a basic IPv4 address 
   $ip = '192.168.1.1';
   $binary_ip = inet_pton($ip);
   echo bin2hex($binary_ip);
?>

Output

Here is the outcome of the following code −

c0a80101%

Example 2

Here, we will test the function with an invalid IPv4 address (192.168.1), which lacks the correct format, while using the inet_pton() function. The function returns false and we print a message showing that the IP address is invalid.

<?php
   // Try to convert an invalid IPv4 address
   $ip = '192.168.1';
   $binary_ip = inet_pton($ip);
   if ($binary_ip === false) {
      echo "Invalid IP address"; 
   }
?> 

Output

This will generate the below output −

Invalid IP address

Example 3

In this program, we define a function called convert_ip() that can handle IPv4 and IPv6 addresses. The function checks whether the address is valid and converts it to binary format. It processes each IP type and outputs the results in hexadecimal. If the IP address is invalid, it generates an error message.

<?php
   // Function to handle both IPv4 and IPv6 addresses
   function convert_ip($ip) {
      $binary_ip = inet_pton($ip);
      if ($binary_ip === false) {
         echo "Invalid IP address: $ip\n";
      } else {
         echo "Binary form of $ip: " . bin2hex($binary_ip) . "\n";
      }
   }

   // IPv4
   $ip1 = '192.168.1.1'; 
   
   // IPv6
   $ip2 = '2001:0db8:85a3:0000:0000:8a2e:0370:7334'; 

   convert_ip($ip1);
   convert_ip($ip2);
?> 

Output

This will create the below output −

Binary form of 192.168.1.1: c0a80101
Binary form of 2001:0db8:85a3:0000:0000:8a2e:0370:7334: 20010db885a3000000008a2e03707334
php_function_reference.htm
Advertisements