Php – Apache mod_geoip – What will $_SERVER[‘GEOIP_COUNTRY_CODE’] / $_SERVER[‘GEOIP_CONTINENT_CODE’] equal if country / continent is unknown

apachegeoipPHP

We use apaches mod_geoip AND php's geoip.so modules to detect the country / continent a user is visiting from.

Initially we just used PHP's, but a few months ago we began using geoip data within our apache configuration so now we use both.

This is redundant.

We are looking at disabling php's geoip.so since we already have the country / continent exposed in $_SERVER['GEOIP_COUNTRY_CODE'] / $_SERVER['GEOIP_CONTINENT_CODE'].

Before we do this, we need to know what these 2 will equal IF geoip is unable to detect the location of the user.

Previously, on the php end we used something along the lines of:

function geoiplookup($ip){
    if($return=@geoip_country_code_by_name($ip))
        return $return;
    return false;
}

So if geoip_country_code_by_name was unable to retrieve the country code, and thus returned false, we set the script-stored country code to (bool)false.

So,

When using apache mod_geoip what does geoip set these 2 variables to if it is unable to retrieve the country code?

Thanks.

Best Answer

We decided to take a look at the mod_geoip source.

From the Changes file:

1.3.3 Aug 13th 2008

  • Fix GEOIP_COUNTRY_CODE is not set, when the region database is used and no country is found. Previously GEOIP_COUNTRY_CODE was set but empty ( Boris Zentner )

So it would appear that if a country / continent CANNOT be found from the provided IP, GEOIP_COUNTRYC_CODE simply WILL NOT be set.

Can someone confirm this as this is a fairly vital part of our code base and we need to make sure.

We are having a hard time looking thru the C code and finding out exactly what is happening on unknown country.


From what we can tell, yes, GEOIP_COUNTRY_CODE is only set if the country can be detected.

From the mod_geoip source:

if (cfg->GeoIPOutput & GEOIP_NOTES) {
    ap_table_set(r->notes, "GEOIP_CONTINENT_CODE", continent_code);
    ap_table_set(r->notes, "GEOIP_COUNTRY_CODE", country_code);
    ap_table_set(r->notes, "GEOIP_COUNTRY_NAME", country_name);
}
Related Topic