3

I'm working on implementing some geoIP functionality to redirect a user away from my .com site to the relevant country domain (.fr, .es, .co.uk ...etc).

I've the following in my index.php to check the users IP:

ini_set('display_errors', 1);
require_once("geoip.inc");
$gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);
$country_code = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
geoip_close($gi);

if($country_code == 'ES')
{
        header('Location: https://www.testsite.es');
}
elseif($country_code == 'GB')
{
        header('Location: https://www.testsite.co.uk');
}
elseif($country_code == 'FR')
{
        header('Location: https://www.testsite.fr');
}
else {
        header('Location: https://www.testsite.com/home');
}

When I check the $country_code variable it is an empty String and as a result the above fails and I always hit https://www.testsite.com/home...

I started delving into the code and noticed that first I call this method:

function geoip_country_code_by_addr($gi, $addr) {

    if ($gi->databaseType == GEOIP_CITY_EDITION_REV1) {
        $record = geoip_record_by_addr($gi, $addr);
        if ($record !== false) {
            return $record->country_code;
        }
    } else {
        $country_id = geoip_country_id_by_addr($gi, $addr);

        if ($country_id !== false) {
            return $gi->GEOIP_COUNTRY_CODES[$country_id];
        }
    }
    return false;
}

which calls:

function geoip_country_id_by_addr($gi, $addr) {
    $ipnum = ip2long($addr);
    return _geoip_seek_country($gi, $ipnum) - GEOIP_COUNTRY_BEGIN;
}

I can't figure out why it keeps failing and returning a '0'? I am using Maxminds geoip.inc php to check the country code.

I've checked that mbstring is enabled within my php.ini file and it is. For some reason it just doesn't find the Country code based on the IP I pass to it. Does anyone have any help in terms of what might be causing this?

Javacadabra
  • 5,578
  • 15
  • 84
  • 152
  • Not every IP address is associated with a location (and the location returned by any geoip service is an educated guess at best). Is this happening with all the addresses you are passing in? – Piskvor left the building Sep 07 '15 at 14:06
  • Yes every single one, I've even tried with my own IP address which I know worked in the past when I used php geo IP with `PECL`. Of course you might say why don't I just use that way but I'm unfortunately unable to go down that route. – Javacadabra Sep 07 '15 at 14:11

1 Answers1

0

just wanted to say that I've resolved the issue. A mistake on my part and probably a sign that I need a break!

Within geoip.inc.php supplied by Maxmind I was initially getting these errors:

Cannot redeclare geoip_country_code_by_name() in geoip.inc on line 438

In order to fix this I simply check if the method is defined and if not I use it as follows:

if (!function_exists('geoip_country_code_by_name')) {
    function geoip_country_code_by_name($gi, $name) {
        $country_id = geoip_country_id_by_name($gi, $name);
        if ($country_id !== false) {
            return $gi->GEOIP_COUNTRY_CODES[$country_id];
        }
        return false;
    }
}

I unfortunately had a minor typo in the above code which prevented the code from executing properly hence returing 0 each and every time.

Javacadabra
  • 5,578
  • 15
  • 84
  • 152