Host and IP resolution

string gethostbyaddr ( string ip_address)

string gethostbyname ( string hostname)

string gethostbynamel ( string hostname)

There are three functions designed to specifically resolve web host information, and these are gethostbyaddr(), gethostbyname() and gethostbynamel() (that is a lower-case L, by the way). All three take one parameter, and the first two complement each other perfectly - gethostbyname() returns the IP address of a server you specify, and gethostbyaddr() returns the domain name of an IP address you specify. Here is an example of their usage:

    $sdip = gethostbyname("");
    $sddomain = gethostbyaddr($sdip);
    print "IP: $sdip\n";
    print "Domain: $sddomain\n";

That should output the following:



By default, gethostbyname() returns the first value IP address for a host, but it is possible that one host has several. This is particularly common for large websites such as, where the site load is typically distributed across seven or eight web servers. If you want more detailed information about all the IP addresses backing a site, use gethostbynamel(), like this:

    $msips = gethostbynamel("");

That should output something like this:

array(8) { [0]=> string(14) "" [1]=> string(14) "" [2]=>
string(14) "" [3]=> string(14) "" [4]=> string(14) "" [5]=> string(14) "" [6]=> string(14) "" [7]=> string(14) "" }


Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: HTTP >>

Previous chapter: Domain resolution functions

Jump to:


Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.