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" }
|
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!
|