Getting Client IP Address with proxy - PHP5.4.4-14 + APACHE 2.2.22-13 + Debian 7 + HTTPS

StackOverflow https://stackoverflow.com//questions/21051293

  •  22-12-2019
  •  | 
  •  

Question

I tried every codes and read all posts with this topic, but none resolved my problem.

The code doesnt get the real IP when the browser option "use proxy for intranet sites" is on.
It shows me the PROXY IP...

Code:

function get_ip_address()
{
    $aa = array ( 'HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', // 'HTTP_HOST', 'REMOTE_ADDR');

    foreach ($aa as $key) {
        if (array_key_exists($key, $_SERVER) === true) {
            foreach (explode(',', $_SERVER[$key]) as $ip) {
                if (filter_var($ip, FILTER_VALIDATE_IP) !== false) {
                    return $ip;
                }
            }
        }
    }
}
#endfunc

echo get_ip_address();
Was it helpful?

Solution

Well, that's the purpose of proxies. You cannot reliably obtain the IP address of your user and, even if you could, do you mean their WAN address? Or one of any number of LAN addresses? It's an intractible problem in the general case.

OTHER TIPS

public function GetClientIP()
{
    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'];
    }
    return $ip;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top