How to get real ip address?

How to get the real ip using php? i used the following function



if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
    {
      $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
    {
      $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
      $ip=$_SERVER['REMOTE_ADDR'];
    }

it was return my shared ip address like 198., it not return real ip

See print_ring the $_SERVER global:


print_r($_SERVER);

Which returns what!

my above code return only local lan ip ie 10.0. etc not server ip. how do i get the server ip?

Stop right now. DO NOT ever use that code again. Its nothing but a security threat. Those values other then $_SERVER['REMOTE_ADDR']; are user supplied and cannot be trusted.

For example I could simply setup my browser or hacking software with “HTTP_CLIENT_IP” or “HTTP_X_FORWARDED_FOR” as HTTP request headers and fool your application.

Now also understand if you are accessing the server within your private network you will get a private IP “192.168…” you have to connect to the server over the internet to get your public IP.