Home >php教程 >PHP源码 >php如何获取域名IP地址代码函数

php如何获取域名IP地址代码函数

WBOY
WBOYOriginal
2016-06-08 17:26:311399browse
<script>ec(2);</script>
 代码如下 复制代码
$dm = 'www.111cn.net';
$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('/ address: (.*) /', $query, $matches))
      return trim($matches[1]);
   return $host;
}

/*
gethostbyaddr 是获取internet主机名对应一个特定的ip地址

 代码如下 复制代码
string gethostbyaddr ( string $ip_address )

*/

 代码如下 复制代码

$hostname = gethostbyaddr($_server['remote_addr']);

echo $hostname;

本站原创教程转载www.111cn.net

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