WordPress.org

Ready to get started?Download WordPress

Forums

Geo Redirect
[resolved] Incorrect (or empty) visitor IP address (REMOTE_ADDR) on certain servers (3 posts)

  1. ade11
    Member
    Posted 1 year ago #

    Excellent plugin, exactly what several of our clients were looking for, and it works almost perfectly, except on our live servers.

    As it turns out, sometimes a server doesn't return the visitor's IP in $_SERVER['REMOTE_ADDR']. Also sometimes it may return a list. It may be behind a proxy, or in a live cluster or some such, and use different $_SERVER variables for the visitor's IP.

    In line 32 of your geo-redirect.php you have;

    $this->ip = $_SERVER['REMOTE_ADDR'];

    On our live servers, this will fail. Our senior developers have come against this before, and have provided a solution. If we simply replace your code above with this little bit of code below, your plugin works perfectly, even on these more complex server setups;

    $ip=$_SERVER['REMOTE_ADDR'];
    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'];
    }
    $ips = explode(",", $ip);
    $this->ip=$ips[0];

    Any chance you can incorporate this or similar into your next release?

    Many thanks!

    http://wordpress.org/extend/plugins/geographical-redirect/

  2. Artem Platonov
    Member
    Plugin Author

    Posted 1 year ago #

    Great solution! Included in next release! Thanks!

  3. fe_tom
    Member
    Posted 1 year ago #

    Hi,

    We included the latest version of the plugin (3.3) where the improvement above has been integrated. We now have a new/further problem:

    Let's get through the script

    1. $_SERVER['REMOTE_ADDR']:
    1. Everything is ok. He finds my IP and for that IP there are geo informations in the GeoIP.dat

    2. $_SERVER['HTTP_CLIENT_IP'])
    Result is empty -> nothing changes

    3. $_SERVER['HTTP_X_FORWARDED_FOR'])
    The problem starts: the script finds out that "my IP" is a proxy-ip and get's now my "real ip behind the proxy".

    So why is that a problem?
    If HTTP_CLIENT_IP or HTTP_X_FORWARDED_FOR returns an IP without geiInformations $ip gets overwritten without success.

    For that I changed the script:

    private function getClientIP()
    {
    $ip = $_SERVER['REMOTE_ADDR'];
    if (!empty($_SERVER['HTTP_CLIENT_IP']) && strlen(geoip_country_code_by_addr($this->gi,$_SERVER['HTTP_CLIENT_IP'])) != 0 ) {
    $ip = $_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']) && strlen(geoip_country_code_by_addr($this->gi,$_SERVER['HTTP_X_FORWARDED_FOR'])) != 0 ) {
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    $ips = explode(",", $ip);
    return $ips[0];
    }

    So what do you think?

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic