0

I've done a lot of research on how to get a user's private IP address. I tried the following:

    $client = $_SERVER['HTTP_CLIENT_IP'];
    $forward = $_SERVER['HTTP_X_FORWARDED_FOR'];
    $remote = $_SERVER['REMOTE_ADDR'];

    if (filter_var( $client, FILTER_VALIDATE_IP))
    {
        $ip_address = $client;
    }
    elseif (filter_var($forward, FILTER_VALIDATE_IP))
    {
        $ip_address = $forward;
    }
    else
    {
        $ip_address = $remote;
    }
    echo $ip_address;
}

I got a public IP address. My site keeps getting the same IP address for devices under a network.

How do I get the private IP address of a user?

Michele La Ferla
  • 6,775
  • 11
  • 53
  • 79
Cee Jaiy
  • 39
  • 7
  • 1
    Possible duplicate of [PHP how to get local IP of system](http://stackoverflow.com/questions/3219178/php-how-to-get-local-ip-of-system). Other than that, it is not possible. You can have procedures in place to identify separate devices using session keys but not know its LAN address. Also note, the IP can be masked so never assume its the **real** socket address. – Jaquarh Feb 17 '17 at 20:24
  • You can only ever get the public IP. Not sure what you are calling the 'private' IP in this case –  Feb 17 '17 at 20:26
  • Those headers are normally only set by a reverse proxy server, not by a NAT router. You can't get the client's private IP. Why would you want it, since many LANs use the same IPs? – Barmar Feb 17 '17 at 20:52
  • You have three `{`s and four `}`s. Isn't the sample code missing something? – Peter Mortensen Feb 17 '17 at 21:19
  • This shouldn't be labeled a duplicate. The post you brought up was 7 years ago. Things have changed since then. This is a valid question. – bagofmilk Aug 15 '17 at 19:34

0 Answers0