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:

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

 

Next chapter: HTTP >>

Previous chapter: Domain resolution functions

Jump to:

 

Home: Table of Contents

Follow us on Identi.ca or Twitter

Username:   Password:
Create Account | About TuxRadar