0

i know this has a million answers here, but what do you do when after all this you still get the Proxy's IP instead of the client's? i have tried a lot of solutions, but none of them work. I always end up with Proxy's IP instead of the client's. any help?

what is my recent try:

<?php
/**
 * Retrieves the best guess of the client's actual IP address.
 * Takes into account numerous HTTP proxy headers due to variations
 * in how different ISPs handle IP addresses in headers between hops.
 */
function get_ip_address() {
    // check for shared internet/ISP IP
    if (!empty($_SERVER['HTTP_CLIENT_IP']) && validate_ip($_SERVER['HTTP_CLIENT_IP'])) {
        return $_SERVER['HTTP_CLIENT_IP'];
    }

    // check for IPs passing through proxies
    if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        // check if multiple ips exist in var
        if (strpos($_SERVER['HTTP_X_FORWARDED_FOR'], ',') !== false) {
            $iplist = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
            foreach ($iplist as $ip) {
                if (validate_ip($ip))
                    return $ip;
            }
        } else {
            if (validate_ip($_SERVER['HTTP_X_FORWARDED_FOR']))
                return $_SERVER['HTTP_X_FORWARDED_FOR'];
        }
    }
    if (!empty($_SERVER['HTTP_X_FORWARDED']) && validate_ip($_SERVER['HTTP_X_FORWARDED']))
        return $_SERVER['HTTP_X_FORWARDED'];
    if (!empty($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']) && validate_ip($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']))
        return $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'];
    if (!empty($_SERVER['HTTP_FORWARDED_FOR']) && validate_ip($_SERVER['HTTP_FORWARDED_FOR']))
        return $_SERVER['HTTP_FORWARDED_FOR'];
    if (!empty($_SERVER['HTTP_FORWARDED']) && validate_ip($_SERVER['HTTP_FORWARDED']))
        return $_SERVER['HTTP_FORWARDED'];

    // return unreliable ip since all else failed
    return $_SERVER['REMOTE_ADDR'];
}

/**
 * Ensures an ip address is both a valid IP and does not fall within
 * a private network range.
 */
function validate_ip($ip) {
    if (strtolower($ip) === 'unknown')
        return false;

    // generate ipv4 network address
    $ip = ip2long($ip);

    // if the ip is set and not equivalent to 255.255.255.255
    if ($ip !== false && $ip !== -1) {
        // make sure to get unsigned long representation of ip
        // due to discrepancies between 32 and 64 bit OSes and
        // signed numbers (ints default to signed in PHP)
        $ip = sprintf('%u', $ip);
        // do private network range checking
        if ($ip >= 0 && $ip <= 50331647) return false;
        if ($ip >= 167772160 && $ip <= 184549375) return false;
        if ($ip >= 2130706432 && $ip <= 2147483647) return false;
        if ($ip >= 2851995648 && $ip <= 2852061183) return false;
        if ($ip >= 2886729728 && $ip <= 2887778303) return false;
        if ($ip >;= 3221225984 && $ip <= 3221226239) return false;
        if ($ip >;= 3232235520 && $ip <= 3232301055) return false;
        if ($ip >= 4294967040) return false;
    }
    return true;
}


$ipad=get_ip_address();
//$ip = $HTTP_SERVER_VARS['HTTP_X_CLUSTER_CLIENT_IP'];
echo    $ipad;


    ?>
  • well, none of those solutions work. THAT IS EXACTLY WHY I POSTED IT AGAIN. :( – Revathy Santhanam Jul 24 '15 at 13:40
  • As you are aware, there are several solutions posted to StackOverflow which worked for other people. So in order to find why they didn't work for you, you need to describe in detail what exactly you did, what you tried to debug the problem on your systems and which results you get. Just saying "I tried it and it didn't work" is not a proper problem description and won't allow us to help you at all. – Holger Just Jul 24 '15 at 13:53
  • Right, I have added the code I tried recently. everytime I run it on the server, the client's IP is blocked by the Proxy's. I read that the client's IP doesnt even reach the server, so I tried to use Javascript to access API's data to get client IP but I am unable to get that value from Javascript into my HTML form i'm using. I tried to give it as value to a hidden input, echo using script tag, all the time I got an empty result. The JS variable value expires by the time it comes to the HTML body. what can i do now? – Revathy Santhanam Jul 24 '15 at 14:19
  • 1
    Well, perhaps **you simply cannot get the client's IP!** There's no magic with which PHP can reach through the internet and interrogate the remote end for its private IP address. You get what you get in the HTTP request. If the proxy does not voluntarily send the client's private address in an HTTP, **there's no way to get it.** For all you care, **the proxy is your client.** – deceze Jul 24 '15 at 14:21
  • What exactly do you need "the client's IP" for?! – deceze Jul 24 '15 at 14:22
  • I see, so there is no other way I can get the Client IP at all? No way possible? – Revathy Santhanam Jul 24 '15 at 14:22
  • i have a HTML form that is supposed to get the Client machine which posts that form's IP and store in MySQL. To know who and from where the form was submitted. – Revathy Santhanam Jul 24 '15 at 14:23
  • So what if the client's private IP is 10.0.1.1? There are **millions** of machines around the globe with that same private IP address. It doesn't tell you anything at all. In fact, the proxy's address tells you a lot more than this. – deceze Jul 24 '15 at 14:24
  • good point, but I was told that with the Public IP address we can gather info about the location and machine submitting the request. isnt that so? – Revathy Santhanam Jul 24 '15 at 14:28
  • This is too broad a topic and there are too many different types of "public" and "private" and "proxies". Suffice it to say that you do not understand what you're asking for well enough. If you did, you'd see that it's mostly pointless what you want. – deceze Jul 24 '15 at 14:30
  • and i tried with JS too (as commented to above solution). is it not possible even then? – Revathy Santhanam Jul 24 '15 at 14:30

0 Answers0