gethostbyname函数获取域名ip地址
【php教程】之gethostbyname函数获取域名ip地址
gethostbyname
(PHP 4, PHP 5, PHP 7)
gethostbyname — 返回主机名对应的 IPv4地址。
说明
string gethostbyname ( string $hostname )
返回主机名 hostname 对应的 IPv4 互联网地址。
参数
hostname
主机名
返回值
成功时返回 IPv4 地址,失败时原封不动返回 hostname 字符串。
php教程如何获取域名ip地址代码函数
$dm = 'www.hftit.com';
$ip = gethostbyname($dm);
echo gethostbyaddr($ip);
echo $ip;
//两个输出结果是一样的,
/*
关于 gethostbyname语法
string gethostbyname ( string $hostname )
返回由主机名指定的互联网主机的ipv4地址
returns the ipv4 address of the internet host specified by hostname
下面有个例这是最好的方法我已经提出,以解决任何主机名称到ip地址时,它的快速,可靠,已超时的支持!一个无效的地址,例如unicode字符串,返回后4?秒,而不是8调用gethostbyname?!它只能与unix虽然。
*/
function getaddrbyhost($host, $timeout = 3) {
$query = `nslookup -timeout=$timeout -retry=1 $host`;
if(preg_match('/naddress: (.*)n/', $query, $matches))
return trim($matches[1]);
return $host;
}
/*
gethostbyaddr 是获取internet主机名对应一个特定的ip地址
string gethostbyaddr ( string $ip_address )
*/
$hostname = gethostbyaddr($_server['remote_addr']);
echo $hostn