-1

I want to get the public ip of a user for socket connection in java. So that i need the user device public ip address for that.

i have made a weservice for that . which is get_ip. But it returning the ip adress of my network, not my device's ip. because while i'm calling this api from my pc with connected my router and while i'm calling this api from my mobile it gives the same ip address.

the code is

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;

I have read some post over statcoverflow like this any many more, But got the same response. please help me.

Artix
  • 1
  • 4
  • 1
    Well, that *is* the public IP. It wouldn’t do you any good to get the local LAN IP anyway, as that’s not accessible from outside the LAN. – deceze Jan 04 '23 at 07:31

1 Answers1

-1

Try in addition $_SERVER['REMOTE_ADDR'] use $_SERVER['HTTP_X_REAL_IP'], should work, as a last resort, you can output the entire $_SERVER variable in the logs to check the IP change on devices, etc. (I correctly understood that it does NOT require an address in the user's local network?)

SvyatX
  • 1
  • 1