dongxing9219 2013-05-31 09:20
浏览 229
已采纳

哪个更可靠gethostbyaddr($ _ SERVER ['REMOTE_ADDR'])或$ _SERVER ['REMOTE_HOST']

I have to get the remote url hostname from the server script, which one of the following is more reliable:

gethostbyaddr($_SERVER['REMOTE_ADDR']) or $_SERVER['REMOTE_HOST']

  • 写回答

2条回答 默认 最新

  • doutangqi4444 2013-05-31 09:26
    关注

    This has nothing to do with reliability. Both variables are just not the same although they may contain the same value under certain circumstances. Let me explain:

    $_SERVER['REMOTE_ADDR']
    

    will in all cases contain the IP address of the remote host, where

    $_SERVER['REMOTE_HOST']
    

    will contain the DNS hostname, if DNS resolution is enabled (if the HostnameLookups Apache directive is set to On, thanks @Pekka). If it is disabled, then $_SERVER['REMOTE_HOST'] will contain the IP address, and this is what you might have observed.

    Your code should look like:

    $host = $_SERVER['REMOTE_HOST'];
    // if both are the same, HostnameLookups seems to be disabled.
    if($host === $_SERVER['REMOTE_ADDR']) {
        // get the host name per dns call
        $host = gethostbyaddr($_SERVER['REMOTE_ADDR'])
    }
    

    Note: If you can control the apache directive, I would advice you to turn it off for performance reasons and get the host name - only if you need it - using gethostbyaddr()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画