How to use HTTP_X_FORWARDED_FOR properly?

前端 未结 6 1586
既然无缘
既然无缘 2020-12-05 17:01

Alright, I have an small authentication issue. My web service allows to connect to my API over HTTP with a username and password, but this connection can also be restricted

相关标签:
6条回答
  • 2020-12-05 17:37

    I like Hrishikesh's answer, to which I only have this to add...because we saw a comma-delimited string coming across when multiple proxies along the way were used, we found it necessary to add an explode and grab the final value, like this:

    $IParray=array_values(array_filter(explode(',',$_SERVER['HTTP_X_FORWARDED_FOR'])));
    return end($IParray);
    

    the array_filter is in there to remove empty entries.

    0 讨论(0)
  • 2020-12-05 17:44

    If you use it in a database, this is a good way:

    Set the ip field in database to varchar(250), and then use this:

    $theip = $_SERVER["REMOTE_ADDR"];
    
    if (!empty($_SERVER["HTTP_X_FORWARDED_FOR"])) {
        $theip .= '('.$_SERVER["HTTP_X_FORWARDED_FOR"].')';
    }
    
    if (!empty($_SERVER["HTTP_CLIENT_IP"])) {
        $theip .= '('.$_SERVER["HTTP_CLIENT_IP"].')';
    }
    
    $realip = substr($theip, 0, 250);
    

    Then you just check $realip against the database ip field

    0 讨论(0)
  • 2020-12-05 17:48

    You can also solve this problem via Apache configuration using mod_remoteip, by adding the following to a conf.d file:

    RemoteIPHeader X-Forwarded-For
    RemoteIPInternalProxy 172.16.0.0/12
    LogFormat "%a %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    
    0 讨论(0)
  • 2020-12-05 17:49

    In the light of the latest httpoxy vulnerabilities, there is really a need for a full example, how to use HTTP_X_FORWARDED_FOR properly.

    So here is an example written in PHP, how to detect a client IP address, if you know that client may be behind a proxy and you know this proxy can be trusted. If you don't known any trusted proxies, just use REMOTE_ADDR

    <?php
    
    function get_client_ip ()
    {
        // Nothing to do without any reliable information
        if (!isset ($_SERVER['REMOTE_ADDR'])) {
            return NULL;
        }
    
        // Header that is used by the trusted proxy to refer to
        // the original IP
        $proxy_header = "HTTP_X_FORWARDED_FOR";
    
        // List of all the proxies that are known to handle 'proxy_header'
        // in known, safe manner
        $trusted_proxies = array ("2001:db8::1", "192.168.50.1");
    
        if (in_array ($_SERVER['REMOTE_ADDR'], $trusted_proxies)) {
    
            // Get the IP address of the client behind trusted proxy
            if (array_key_exists ($proxy_header, $_SERVER)) {
    
                // Header can contain multiple IP-s of proxies that are passed through.
                // Only the IP added by the last proxy (last IP in the list) can be trusted.
                $proxy_list = explode (",", $_SERVER[$proxy_header]);
                $client_ip = trim (end ($proxy_list));
    
                // Validate just in case
                if (filter_var ($client_ip, FILTER_VALIDATE_IP)) {
                    return $client_ip;
                } else {
                    // Validation failed - beat the guy who configured the proxy or
                    // the guy who created the trusted proxy list?
                    // TODO: some error handling to notify about the need of punishment
                }
            }
        }
    
        // In all other cases, REMOTE_ADDR is the ONLY IP we can trust.
        return $_SERVER['REMOTE_ADDR'];
    }
    
    print get_client_ip ();
    
    ?>
    
    0 讨论(0)
  • 2020-12-05 17:59

    You can use this function to get proper client IP:

    public function getClientIP(){       
         if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)){
                return  $_SERVER["HTTP_X_FORWARDED_FOR"];  
         }else if (array_key_exists('REMOTE_ADDR', $_SERVER)) { 
                return $_SERVER["REMOTE_ADDR"]; 
         }else if (array_key_exists('HTTP_CLIENT_IP', $_SERVER)) {
                return $_SERVER["HTTP_CLIENT_IP"]; 
         } 
    
         return '';
    }
    
    0 讨论(0)
  • 2020-12-05 18:01

    HTTP_CLIENT_IP is the most reliable way of getting the user's IP address. Next is HTTP_X_FORWARDED_FOR, followed by REMOTE_ADDR. Check all three, in that order, assuming that the first one that is set (isset($_SERVER['HTTP_CLIENT_IP']) returns true if that variable is set) is correct. You can independently check if the user is using a proxy using various methods. Check this out.

    0 讨论(0)
提交回复
热议问题