I need to get the public IP address of the client.
I have tried the
getenv('REMOTE_ADDR') and also $_SERVER['REMOTE_ADDR']
but it returns only the private ip ...
I need to get the public IP address of the client.
I have tried the
getenv('REMOTE_ADDR') and also $_SERVER['REMOTE_ADDR']
but it returns only the private ip ...
Ive used this:
function GetIp(){
//IP ADDRESS
$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');
else
$ipaddress = 'UNKNOWN';
$AgentIp = $ipaddress;
return $AgentIp;
}
I am looking for the same problem and found great solution API based.
To get the public IP address we can can use the below code:
<?php
$ip = file_get_contents('https://api.ipify.org');
echo "My public IP address is: " . $ip;
?>
API Site Url: https://www.ipify.org/
It's too late to asnwer on this but it may be halpful for others.