Home  >  Article  >  Backend Development  >  How to get domain name IP address code function in php

How to get domain name IP address code function in php

高洛峰
高洛峰Original
2016-11-29 16:02:451411browse

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;

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn