Breaking News
Home / Web / PHP / PHP Snippets / How to resolve Hostname to IP or viceversa in PHP?

How to resolve Hostname to IP or viceversa in PHP?

PHP language provides two convenient functions to achieve this.

– gethostbyaddr()
-gethostbyname()

The gethostbyaddr() and gethostbyname() functions will return the hostname or IP address of a given machine, respectively.

The syntax of these function is as follows
gethostbyaddr(“IP Address”);
gethostbyname(“hostname”);

  1. <?php
  2.  
  3. // set iP address
  4. $ip = "74.125.224.147";
  5.  
  6. // set hostname
  7. $hostname = "www.google.com";
  8.  
  9. $resolved_hostname = gethostbyaddr($ipadd);
  10.  
  11. $resolved_ip = gethostbyname($hostname);
  12.  
  13. echo "The IP address $ip resolves to $resolved_hostname";
  14. echo "The Hostname $hostname resolves to $resolved_ip";
  15. ?>
[pb_builder]

Check Also

How to list files in a directory in PHP?

<?php if ($handle = opendir(‘/path/to/directory’)) {     echo "Directory handle: $handlen";     echo …

Advertisment ad adsense adlogger