PHPackages                             terminal42/contao-geoip2-country - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. terminal42/contao-geoip2-country

ActiveContao-bundle[Utility &amp; Helpers](/categories/utility)

terminal42/contao-geoip2-country
================================

Detect country of visitor and protect elements in Contao using MaxMind GeoIP2 database

1.4.2(11mo ago)61.6k1[2 issues](https://github.com/terminal42/contao-geoip2-country/issues)1MITPHPPHP ^8.1CI passing

Since Sep 24Pushed 11mo ago3 watchersCompare

[ Source](https://github.com/terminal42/contao-geoip2-country)[ Packagist](https://packagist.org/packages/terminal42/contao-geoip2-country)[ GitHub Sponsors](https://github.com/terminal42)[ Fund](https://ko-fi.com/terminal42)[ RSS](/packages/terminal42-contao-geoip2-country/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (22)Versions (19)Used By (1)

terminal42/contao-geoip2-country
================================

[](#terminal42contao-geoip2-country)

This Contao extension finds the country from the client IP. The website can then be customized based on this information.

This extension requires the [MaxMind GeoIP2 database](https://www.maxmind.com/en/geoip2-databases), either the *GeoIP2 Country* or *GeoLite2 Country* database. Be aware that you might need a commercial license of this product depending on your use case!

Features
--------

[](#features)

1. **Limit content based on the user country**
    By default, visibility for pages, articles, content elements and front end modules can be set for the country. For each content, you can either show only to or hide it from a list of countries.
2. **Root page routing based on the user country**
    GeoIP routing in the page tree allows you to define what root page a visitor will be redirected to based on their country.
3. **Symfony HTTP Reverse Proxy**
    Integrated support for the Symfony HTTP Reverse Proxy allows a page to be cached for each country by using `Vary` headers. Without a supported reverse proxy, responses with country-specific content are automatically set to `Cache-Control: private`.

    This will be automatically configured for you in a Contao Managed Edition.
4. **Default country for members**
    The detected country is set as the default country for new members, so the registration front end module already has the country pre-selected.
5. **Support for `terminal42/contao-countryselect`**
    If the `countryselect` form field is added to a form, the default option is automatically set to the visitors country.
6. **Automatic update of GeoIP2-Country database**
    Using the Contao cron scheduler, if you configure the necessary API credentials.

### Note on page visibility

[](#note-on-page-visibility)

If the visibility of a root page is configured, it also affects all its subpages. This means if a root page is not available for a country, none of the pages in this tree will be available.

Enabling this on the fallback root page can lead to unwanted consequences, because the user will not be redirected to **any** page if none of the preferred languages match the browser!

Installation
------------

[](#installation)

Choose the installation method that matches your workflow!

### Installation via Contao Manager

[](#installation-via-contao-manager)

Search for `terminal42/contao-geoip2-country` in the Contao Manager and add it to your installation. Finally, update the packages.

### Manual installation

[](#manual-installation)

Add a composer dependency for this bundle. Therefore, change in the project root and run the following:

```
composer require terminal42/contao-geoip2-country
```

Depending on your environment, the command can differ, i.e. starting with `php composer.phar …` if you do not have composer installed globally.

Then, update the database via the `contao:migrate` command or the Contao install tool.

#### HTTP Reverse Proxy

[](#http-reverse-proxy)

If you do not use the Contao Managed Edition, you can manually register the CacheHeaderSubscriber when using the Symfony Reverse Proxy including `friendsofsymfony/http-cache`.

Configuration
-------------

[](#configuration)

### MaxMind GeoIP2 database

[](#maxmind-geoip2-database)

Install the binary MMDB file and configure its path in the `GEOIP2_DATABASE` environment variable (e.g. through your `.env`/`.env.local` file).

### Bundle configuration

[](#bundle-configuration)

**Default configuration:**

```
terminal42_geoip2_country:
    database_path: '%env(GEOIP2_DATABASE)%'
    fallback_country: XX
    dca_tables: [tl_content, tl_article, tl_module, tl_page]
    credentials: '%env(GEOIP2_AUTH)%'
    update_interval: weekly
```

- **database\_path:** Path to the MMDB file. Defaults to the `GEOIP2_DATABASE` environment variable. Be aware that this setting does not apply to the HTTP Reverse Proxy!
- **fallback\_country:** The default country if a visitors IP cannot be detected (e.g. applies to localhost as well). *XX* is the United Nations standard for *unknown country*, but by entering a valid [ISO 3166-1 alpha-2 code](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) unknown visitors will see a specific content.
- **dca\_tables:** configures which elements allow the country restrictions. Changing this will add the DCA fields to the given table(s), but for any but the default tables you will need to implement your own visibility checks!
- **update\_credentials:** Required for automatic update of the GeoIP2-Country database. Defaults to the `GEOIP2_AUTH` environment variable. Use your MaxMind account ID and license key in basic-authentication format, e.g. "1234:your-key".
- **update\_interval:** How often the database will be updated automatically, if you add the necessary `update_credentials`. Defaults to "weekly". Can also be set to an empty value if you want to manually update using the Contao console `geoip2:database-update` command.

Updating the MaxMind GeoIP2 database
------------------------------------

[](#updating-the-maxmind-geoip2-database)

Detecting the country from IP requires an up-to-date information source, as IPs change all the time. If you configure the credentials or `GEOIP2_AUTH` variable, this extension can automatically update the database for you.

You can also use [MaxMind's Automatic Update Support](https://dev.maxmind.com/geoip/geoipupdate/) to keep your database up-to-date.

GeoIP Routing
-------------

[](#geoip-routing)

GeoIP Routing is a powerful feature to define which root page / language a visitor will see, overriding the default language routing of Contao. This is mostly useful if you have country-specific websites, but the default browser language matching is unreliable.

As an example, you might have root pages for "de", "en" and "de-CH". You want visitors from Switzerland to prefer "de-CH" over "de", but otherwise keep the Contao routing.

Using *GeoIP2 Routing*, add a configuration for Switzerland, and select the "de-CH" root page. If you still want to allow english browsers to receive the "en" page, also select that one. Order the pages so the fallback (the one that will be serverd to e.g. french visitors) is at the top.

If you do not define any other rule, Swiss visitors will receive the configured page, the rest of the world will receive one of the tree root pages depending on their browser. If you want to prevent the rest of the world from receiving the "de-CH" page, add a *Fallback* routing for "de" and "en" only.

Access the current user country
-------------------------------

[](#access-the-current-user-country)

To retrieve the current country in your own code, inject the `Terminal42\Geoip2CountryBundle\CountryProvider` service into your class. Then use the `getCurrentCountry` method and pass the request object to it.

**Example in a Contao `AbstractFrontendModule` controller:**

```
