anjanesh
anjanesh

Reputation: 4251

Maxmind's GeoLite2 Continent code

I'm using MaxMind's free GeoLite2 database.
According to their code, I can get the country's ISO code.
print($record->country->isoCode . "\n"); // 'US'
But how do I get the continent code ? I'm more specifically looking for EU detection.

Upvotes: 1

Views: 2009

Answers (3)

Professor
Professor

Reputation: 1

SYNTAX for htaccess GeoLite2 Free Geolocation Data, 2024/06 MaxMind DB Apache 2.2+ Module directives from https://maxmind.github.io/mod_maxminddb

MaxMindDBFile <custom_dbname> <server_path_to_database_file>

MaxMindDBEnv <environment_variable> <custom_dbname/path_to_keys-in_db>

SetEnvIf <environment_variable> <desired_code>

Complete working sample with continent and country for htaccess:

<IfModule mod_maxminddb.c>

MaxMindDBEnable On
MaxMindDBFile DB /var/lib/GeoIP/GeoLite2-Country.mmdb
MaxMindDBEnv MM_CONTINENT_CODE DB/continent/code
MaxMindDBEnv MM_COUNTRY_CODE DB/country/iso_code
SetEnvIf MM_CONTINENT_CODE ^(EU|NA) AllowContinent
SetEnvIf MM_COUNTRY_CODE ^(AU|NZ) AllowCountry
Order Allow,Deny
Allow from env=AllowContinent
Allow from env=AllowCountry

</IfModule>

Upvotes: 0

user3277192
user3277192

Reputation:

EU

The list of EU (European Union) countries is:

  • AT, Austria
  • BE, Belgium
  • BG, Bulgaria
  • CR, Croatia
  • CY, Republic of Cyprus
  • CZ, Czech Republic
  • DK, Denmark
  • EE, Estonia
  • FI, Finland
  • FR, France
  • DE, Germany
  • GR, Greece
  • HU, Hungary
  • IE, Ireland
  • IT, Italy
  • LV, Latvia
  • LI, Lithuania
  • LU, Luxembourg
  • MT, Malta
  • NL, Netherlands
  • PL, Poland
  • PT, Portugal
  • RO, Romania
  • SK, Slovakia
  • SI, Slovenia
  • SP, Spain
  • SE, Sweden
  • GB, United Kingdom

example of what I use:

if (isset($_SERVER['GEOIP_COUNTRY_CODE'])) {
  $cc=$_SERVER['GEOIP_COUNTRY_CODE'];
} else {
  $cc='??';
}
$eu=array('AT', 'BE', 'BG', 'HR', 'CY', 'CZ', 'DK', 'EE', 'FI', 'FR', 'DE', 'GR', 'HU', 'IE', 'IT', 'LV', 'LT', 'LU', 'MT', 'NL', 'PL', 'PT', 'RO', 'SK', 'SI', 'SP', 'SE', 'GB');
if (in_array($cc,$eu)) {
   //inside EU
} else {
   //outside EU
}

EEA

Depending on what you seek to do (considering this is asked after GDPR became enforceable), you might want to test for EEA (European Economic Area) instead.

The EEA is:

  • all EU members
  • NO, Norway
  • IS, Iceland
  • LI, Liechtenstein

This is what I use with the full geoip:

if (isset($_SERVER['GEOIP_COUNTRY_CODE'])) {
  $cc=$_SERVER['GEOIP_COUNTRY_CODE'];
} else {
  $cc='??';
}
$eea=array('AT', 'BE', 'BG', 'HR', 'CY', 'CZ', 'DK', 'EE', 'FI', 'FR', 'DE', 'GR', 'HU', 'IE', 'IT', 'LV', 'LT', 'LU', 'MT', 'NL', 'PL', 'PT', 'RO', 'SK', 'SI', 'SP', 'SE', 'GB', 'NO', 'IS', 'LI');
if (in_array($cc,$eea)) {
   //inside EEA
} else {
   //outside EEA
}

Special cases

You need to deal with a number of special cases that depending on your intended use are to be included or not:

Part of the EU, but with their own ISO 2 letter code

  • GI, Gibraltar (~UK)
  • AX, Åland Islands (~Finland)

Note there are more of these, but they have no ISO country code of their own ...

These part of the EU and EEA, and have their own 2 letter country code.

Outermost regions

  • GF, French Guiana (~France)
  • GP, Guadeloupe (~France)
  • MQ, Martinique (~France)
  • YT, Mayotte (~France)
  • RE, Réunion (~France)
  • MF, Saint Martin (~France)

It gets complex with these, you should really read up on each for the specific reason you seek to define the EU if they're in or out. Also note that these tend to change sometimes without all that much press coverage.

Overseas countries and territories

There's even more of these, but they should not be considered part of the EU for most use cases, still do check them out if you have specific reasons.

Enclaves

These tend to not have their own country code, but sometimes for some purposes can have different rules applied to them nonetheless.

Examples are isolated parts of e.g. Germany or Spain, but are located inside e.g. Switzerland or Morocco.

EFTA

For completeness: Switzerland (CH) is neither a member of the EU nor of the EEA, but it is a member of the EFTA (European Free Trade Association).

Micro states

Liechtenstein is member of the EEA, but there's a few other that are neither part of the EU nor the EEA. Still, for practical reasons they do have various relations with the EU (such as being part of the EURO zone, being inside the Schengen zone, charging VAT, etc.)

  • AD, Andorra
  • LI, Liechtenstein
  • MC, Monaco
  • SM, San Marino
  • VC, Vatican City

Continent

If you do seek the continent you will need to add a lot more countries, and you'll have to deal with countries that are not solely in Europe (e.g. Turkey, Russia).

Maxmind's "EU" country code

Maxmind sometimes uses the country code "EU" when it cannot determine which country it actually is. This "EU" refers to Europe, not the European Union (for which the EU country code is actually reserved).

More info:

https://en.wikipedia.org/wiki/Special_member_state_territories_and_the_European_Union https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 https://en.wikipedia.org/wiki/Microstates_and_the_European_Union https://dev.maxmind.com/faq/what-are-the-eu-europe-and-ap-asia-pacific-entries/

Upvotes: 1

Greg Oschwald
Greg Oschwald

Reputation: 1735

If you are just looking for whether the country is a member state of the European Union, MaxMind provides this:

$record->country->isInEuropeanUnion

If you want the continent code, you can get it with:

$record->continent->code

However, please note that not all countries in Europe are part of the European Union.

Upvotes: 1

Related Questions