-1

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 ...

Saty
  • 22,443
  • 7
  • 33
  • 51
Vijayakumar
  • 1
  • 1
  • 1
  • Are you running the server on your local machine? – lifeisfoo Sep 05 '15 at 07:12
  • IP addresses are of no real value to a server, since the topology they make sense in relation to is not under the servers control. So addresses cannot be trusted. So what for? – arkascha Sep 05 '15 at 07:12
  • 4
    possible duplicate of [Get the client IP address using PHP](http://stackoverflow.com/questions/15699101/get-the-client-ip-address-using-php) – Shehary Sep 05 '15 at 07:13
  • C'mon man, the duplicate question is almost the exact same wording as your question! – ScottMcGready Sep 05 '15 at 11:46

2 Answers2

2

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;

}
MrK
  • 1,060
  • 9
  • 23
2

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.

Pankaj Pareek
  • 3,806
  • 2
  • 31
  • 42