2019-08-18 19:33
浏览 244


I'm trying to get the ip address of the client so that they only use one account per device. However, when I use the following code, I found that the same ip address returned from the computers in the same network.

function get_client_ip() {
        $ipaddress = '';
        if (getenv('HTTP_CLIENT_IP'))
            $ipaddress = getenv('HTTP_CLIENT_IP');
        else if(getenv('HTTP_X_FORWARDED_FOR'))
            $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
        else if(getenv('HTTP_X_FORWARDED'))
            $ipaddress = getenv('HTTP_X_FORWARDED');
        else if(getenv('HTTP_FORWARDED_FOR'))
            $ipaddress = getenv('HTTP_FORWARDED_FOR');
        else if(getenv('HTTP_FORWARDED'))
            $ipaddress = getenv('HTTP_FORWARDED');
        else if(getenv('REMOTE_ADDR'))
            $ipaddress = getenv('REMOTE_ADDR');
            $ipaddress = 'UNKNOWN';

        return $ipaddress;

How can I get the correct ip address from different devices?

图片转代码服务由CSDN问答提供 功能建议

我正在尝试获取客户端的IP地址,以便每个设备只使用一个帐户。 但是 ,当我使用下面的代码时,我发现从同一网络中的计算机返回相同的IP地址。

  function get_client_ip  (){
 $ ipaddress =''; 
 $ ipaddress = getenv('HTTP_CLIENT_IP'); 
 if if(getenv('HTTP_X_FORWARDED_FOR'))
 $ ipaddress  = getenv('HTTP_X_FORWARDED_FOR'); 
 if if(getenv('HTTP_X_FORWARDED'))
 $ ipaddress = getenv('HTTP_X_FORWARDED'); 
 if if(getenv('HTTP_FORWARDED_FOR'))
 $ ipaddress = getenv  ('HTTP_FORWARDED_FOR'); 
 if if(getenv('HTTP_FORWARDED'))
 $ ipaddress = getenv('HTTP_FORWARDED'); 
 if if(getenv('REMOTE_ADDR'))
 $ ipaddress = getenv('  REMOTE_ADDR'); 
 $ ipaddress ='  UNKNOWN'; 
返回$ ipaddress; 

如何从不同设备获取正确的IP地址? < / DIV>

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • donglu5041 2019-08-18 19:57

    Most likely those computers are going from network through the router with NAT enabled. In that case you will see only ip address of that router, nothing you can do with it.

    But you can check combination of ip address and user-agent header:


    Unfortunately, there is no garaunteed way to 100% check the origin of request, but combination of ip address and user-agent could work for simple scenario.

    打赏 评论

相关推荐 更多相似问题