Hudzilla.org - the homepage of Paul Hudson
Contents > Networks Wish List | Report Bug | About Me ]

15.3     Host and IP resolution: gethostbyaddr(), gethostbyname(), and gethostbynamel()

This is NOT the latest copy of this book; click here for the latest version.

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:

<?php
    $sdip
= gethostbyname("slashdot.org");
    
$sddomain = gethostbyaddr($sdip);
    print
"IP: $sdip\n";
    print
"Domain: $sddomain\n";
?>

That should output the following:

IP: 66.35.250.150

Domain: slashdot.org

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 microsoft.com, 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:

<?php
    $msips
= gethostbynamel("www.microsoft.com");
    
var_dump($msips);
?>

That should output something like this:

array(8) { [0]=> string(14) "207.46.156.252" [1]=> string(14) "207.46.144.222" [2]=>
string(14) "207.46.250.252" [3]=> string(14) "207.46.245.156" [4]=> string(14) "207.46.144.188" [5]=> string(14) "207.46.134.221" [6]=> string(14) "207.46.244.188" [7]=> string(14) "207.46.249.252" }




<< 15.2 Domain resolution functions: dns_check_record(), dns_get_mx(), and dns_get_record()   15.4 HTTP >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Be the first to add a comment to this chapter!



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is nine plus three?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow