Search code examples
phpgeoipmaxmind

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.


Solution

  • 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/