Look at the following code first, the two output results are the same:
$dm = 'www.phpfensi.com';
$ip = gethostbyname($dm);
echo gethostbyaddr($ip);
echo $ip;
About gethostbyname syntax:
string gethostbyname ( string $hostname )
returns the ipv4 address of the internet host specified by hostname
Here is an example This is the best method I've come up with to resolve any hostname to ip address, it's fast, reliable, has timeout support for an invalid address, e.g. a unicode string, and returns after 4 seconds instead of 8 Calling gethostbyname? It only works with unix though, the code is as follows:
function getaddrbyhost($host, $timeout = 3) {
$query = `nslookup -timeout=$timeout -retry=1 $host`;
if (preg_match('/ address: (.*) /', $query, $matches))
return trim($matches[1]);
return $host;
}
gethostbyaddr is to get the internet host name Corresponding to a specific IP address, the code is as follows:
string gethostbyaddr (string $ip_address)
$hostname = gethostbyaddr($_server['remote_addr']);
echo $hostname;