如何在PHP中获取设备的公有ip地址[复制]

r9f1avp5  于 2023-01-08  发布在  PHP
关注(0)|答案(1)|浏览(205)
    • 此问题在此处已有答案**:

(32个答案)
3天前关闭。
我想得到一个java中套接字连接的用户的公共ip。所以我需要用户设备的公共ip地址。
我已经做了一个weservice.这是get_ip.但它返回我的网络的ip地址,而不是我的设备的ip.因为当我调用这个api从我的pc与连接我的路由器,当我调用这个api从我的手机它给相同的ip地址.
代码为

header('Content-type: application/json');
 $error->code = -200;

    $ipaddress = '';
    if (getenv('HTTP_CLIENT_IP'))
        $error->ip7 = getenv('HTTP_CLIENT_IP');
     if(getenv('HTTP_X_FORWARDED_FOR'))
        $error->ip8 = getenv('HTTP_X_FORWARDED_FOR');
   if(getenv('HTTP_X_FORWARDED'))
        $error->ip9 = getenv('HTTP_X_FORWARDED');
   if(getenv('HTTP_FORWARDED_FOR'))
        $error->ip10 = getenv('HTTP_FORWARDED_FOR');
    if(getenv('HTTP_FORWARDED'))
       $error->ip11 = getenv('HTTP_FORWARDED');
     if(getenv('REMOTE_ADDR'))
        $error->ip12 = getenv('REMOTE_ADDR');
  
 
 
   $ipaddress = '';
    if (isset($_SERVER['HTTP_CLIENT_IP']))
        $error->ip1 = $_SERVER['HTTP_CLIENT_IP'];
     if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
      $error->ip2 = $_SERVER['HTTP_X_FORWARDED_FOR'];
   if(isset($_SERVER['HTTP_X_FORWARDED']))
      $error->ip3 = $_SERVER['HTTP_X_FORWARDED'];
    if(isset($_SERVER['HTTP_FORWARDED_FOR']))
       $error->ip4 = $_SERVER['HTTP_FORWARDED_FOR'];
    if(isset($_SERVER['HTTP_FORWARDED']))
        $error->ip5 = $_SERVER['HTTP_FORWARDED'];
    if(isset($_SERVER['REMOTE_ADDR']))
       $error->ip6 = $_SERVER['REMOTE_ADDR'];
  
  
 
 
 $json = json_encode($error);
  
 echo $json;
  return;

我已经读了一些职位超过statcoverflow像this任何更多,但得到了相同的React。请帮助我。

db2dz4w8

db2dz4w81#

尝试另外$_SERVER['REMOTE_ADDR']使用$_SERVER['HTTP_X_REAL_IP'],应该工作,作为最后的手段,您可以输出整个$_SERVER变量在日志中检查设备上的IP更改等(我正确地理解,它不需要在用户的本地网络中的地址?)

相关问题