Quantcast
Channel: Developer Feed - Snippet
Viewing all articles
Browse latest Browse all 178

How to resolve Hostname to IP or viceversa in PHP?

$
0
0

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. ?>

Viewing all articles
Browse latest Browse all 178

Trending Articles