-1

I use the below condition., but it display the public ip address of server. here i required the actual ip address of client., please help me.

if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
    $ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
    $ip = $_SERVER['REMOTE_ADDR'];
}
  • 4
    Checkout this SO question: http://stackoverflow.com/questions/3003145/how-to-get-the-client-ip-address-in-php – Criesto Aug 25 '15 at 09:57
  • Possible duplicate. You can probably find [your answer here](http://stackoverflow.com/a/3003233/1369528) – LaVomit Aug 25 '15 at 10:03
  • Typically your attempt to find out the clients IP address is an indicator for a problem in your architecture. Since it is impossible to really find out such a value (technical reasons) that value obtained is of very questionable worth. Therefore relying on makes only limited sense, be it for documentation of authentication purposes. Think about if what you try to do really does make sense... – arkascha Aug 25 '15 at 10:03
  • Don't you try to connect to the server from the same (development) machine? – David Ferenczy Rogožan Aug 25 '15 at 10:06

2 Answers2

1

Use this function

 function get_client_ip() {
$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';
return $ipaddress;

}

Drop Shadow
  • 845
  • 3
  • 12
  • 28
0

Try these:

$ip = getenv('HTTP_CLIENT_IP')?:
getenv('HTTP_X_FORWARDED_FOR')?:
getenv('HTTP_X_FORWARDED')?:
getenv('HTTP_FORWARDED_FOR')?:
getenv('HTTP_FORWARDED')?:
getenv('REMOTE_ADDR');
kenorb
  • 155,785
  • 88
  • 678
  • 743