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, 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!
-
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. -
Symfony HTTP Reverse Proxy
Integrated support for the Symfony HTTP Reverse Proxy allows a page to be cached for each country by usingVary
headers. Without a supported reverse proxy, responses with country-specific content are automatically set toCache-Control: private
.This will be automatically configured for you in a Contao Managed Edition.
-
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. -
Support for
terminal42/contao-countryselect
If thecountryselect
form field is added to a form, the default option is automatically set to the visitors country.
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!
Choose the installation method that matches your workflow!
Search for terminal42/contao-geoip2-country
in the Contao Manager and add it to your installation. Finally, update the
packages.
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.
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
.
Install the binary MMDB file and configure its path in the GEOIP2_DATABASE
environment variable
(e.g. through your .env
/.env.local
file).
Default configuration:
terminal42_geoip2_country:
database_path: %env(GEOIP2_DATABASE)%
fallback_country: XX
dca_tables: [tl_content, tl_article, tl_module, tl_page]
-
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 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!
Detecting the country from IP requires an up-to-date information source, as IPs change all the time. We recommend to use MaxMind's Automatic Update Support to keep your database up-to-date.
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:
<?php
namespace App\Controller;
use Contao\CoreBundle\Controller\FrontendModule\AbstractFrontendModuleController;
use Contao\CoreBundle\Twig\FragmentTemplate;
use Contao\ModuleModel;
use Symfony\Component\HttpFoundation\Request;
use Terminal42\Geoip2CountryBundle\CountryProvider;
class FooController extends AbstractFrontendModuleController
{
public function __construct(
private readonly CountryProvider $countryProvider
) {
}
public function getResponse(FragmentTemplate $template, ModuleModel $model, Request $request): Response
{
// Only show content to Switzerland
if ('CH' !== $this->countryProvider->getCurrentCountry($request)) {
return new Response();
}
return $template->getResponse();
}
}
Thanks to Burki & Scherer AG (@Tsarma) for the original implementation and supporting the development.
This bundle is released under the MIT