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

318 Views Asked by At

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();
2

There are 2 best solutions below

0
On BEST ANSWER

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.

3
On
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;
}