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.