百科狗-知识改变命运!
--

gethostbyaddr() - php 网络函数

梵高12个月前 (11-21)阅读数 13#技术干货
文章标签地址

gethostbyaddr()

(PHP 4, PHP 5, PHP 7)

获取指定的IP地址对应的主机名

说明

gethostbyaddr(string $ip_address): string

返回指定的IP地址($ip_address)对应的主机名。

参数

$ip_address

gethostbyaddr() - php 网络函数

主机的IP地址。

返回值

成功则返回主机名;失败则原样输出(输出IP地址);如果输入的格式不正常,则返回FALSE

范例

一个使用gethostbyaddr()的简单例子

参见

  • gethostbyname() 返回主机名对应的 IPv4地址。
  • gethostbynamel() 获取互联网主机名对应的 IPv4 地址列表
Sometimes when using $_SERVER['HTTP_X_FORWARDED_FOR'] OR $_SERVER['REMOTE_ADDR'] more than 1 IP address is returned, for example '155.240.132.261, 196.250.25.120'. When this string is passed as an argument for gethostbyaddr() PHP gives the following error: Warning: Address is not a valid IPv4 or IPv6 address in... 
To work around this I use the following code to extract the first IP address from the string and discard the rest. (If you wish to use the other IPs they will be in the other elements of the $ips array).
if (strstr($remoteIP, ', ')) {
  $ips = explode(', ', $remoteIP);
  $remoteIP = $ips[0];
}
Hope this helps someone :)
The problem of broken DNS servers was causing me a problem because i had a page for user statistics that required around 20 reverse dns lookups to be done, and even as many as 5/6 of them being broken was causing a huge delay in loading the page. so i wrote a function that uses a UDP socket to talk directly to the DNS server (instead of going via the normal gethostbyaddr function) this let me set a timeout.
The only requirement is that your DNS server must be able to do recursive lookups, it wont go to other DNS servers if its told to... and of course you need to know your DNS servers IP address :-)

This could be expanded quite a bit and improved but it works and i've seen quite a few people trying various methods to achieve something like this so i decided to post it here. on most servers it should also be more efficient than other methods such as calling nslookup because it doesn't need to run external programs
Note: I'm more a C person than a PHP person, so just ignore it if anything hasn't been done the *recomended* way :-)
gethostbyaddr() doesn't seem to be able to resolve ip6.int
(ipv6) adresses, so I made a function that can, and works
just like the normal gethostbyaddr().
You need dig and ipv6calc, dig should come with most
distributions, if not, install bind from http://www.isc.org.
ipv6calc can be found at http://www.bieringer.de/linux/IPv6/ipv6calc/index.html.
 function gethostbyaddr6($ip6) {
 $ipv6calc = "/bin/ipv6calc";
 $dig = "/usr/bin/dig";
 $file = popen($ipv6calc." --in ipv6addr --out revnibbles.int ".escapeshellarg($ip6), r);
 $ip = fread($file, 128);
 pclose($file);
 if ((substr($ip, 0, 5) == "Error") || (!$ip)) return "Address is not a valid IPv6 address";
 $file = popen($dig." ptr ".$ip, r);        
  while (!feof ($file)) {
  $buffer = fgets($file, 128);
  if (substr($buffer, 0, 1) == ";") continue;
  $buffer = explode(" ", $buffer);
  if ($buffer[3] == "PTR") {
   $host = substr(trim($buffer[4]), 0, -1);
   pclose($file);
   return $host;
  }
  }
 pclose($file);
 return $ip6;
 }
 echo gethostbyaddr6($_SERVER[REMOTE_ADDR]);
If all else fails, but you have shell access, Unix/Linux servers can use this for a timeout response:
shell_exec('host -W 2 0.0.0.0');
Where 0.0.0.0 is of course the IP, and '2' is the number of seconds for the timeout. This returns a more detailed string of info, with some additional text which might vary depending on the system, so if you want a string with the hostname and nothing else, you'll have to do some substring cutting. There should be an equivalent of 'host' for Windows users to execute, but it isn't my platform.
I discovered that gethostbyaddr sometimes returned the same host name with some uppercase letters in it and sometimes with all lowercase letters.
example:
d54c34fa1.access.example.com
d54C34FA1.access.example.com
This is probably not the fault of gethostbyaddr, but this can be a problem when comparing or storing, because it will give two entries instead of one.
A simple solution to this is to use strtolower on the host name.
Here's a simple function that uses Dig to obtain the hostname for a given IP address. If no hostname can be found it returns the IP again.
Works only on linux / unix, or whatever other platform with dig installed as a command line utility. 
Since my little ISP thing isn't globally acceptable, here's an update. 
If you have found the host of the ip, the shortest way to cut it not to display the full hostname to the public would be:
$host = substr($host, strpos($host, ".") + 1);
P.S. strpos() can also be easily used if you want to put "*" for every simbol you ommit, like so:
$os = strpos($host, ".");
$host = substr($host, $os);
$host = str_repeat("*", $os) . $host;
--McTrafik
I previously used something very similar to what god@weaponzero.f2s.com posted but found it to be quite tedious for getting the 'nicehost'. This method below is a lot cleaner, and it also works for numeric addresses.
function nicehost($host) {
  if (ereg('^([0-9]{1,3}\.){3}[0-9]{1,3}$', $host)) {
    return(ereg_replace('\.[0-9]{1,3}$', '.*', $host));
  } else {
    return(ereg_replace('^.{' . strpos($host, '.') . '}', '*', $host));
  }
}
Be cautious when looking up many hostnames. If your DNS server is slow to respond, you may have to pump up your Max execution time for your scripts, otherwise, it will timeout. I found that even 3 unresolvable hosts can cause a 30 second delay in processing.
Going through numerous tests, the following results are concluded:

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)