This project is not covered by Drupal’s security advisory policy.

Visitor Attributes GeoIP gives Visitor Attributes a visitor's country from a MaxMind database file on your own server. It adds a geo provider, "MaxMind database (GeoLite2 or GeoIP2)", to Visitor Attributes' settings. The country attribute, and the continent and eu_visitor attributes derived from it, then come from the database, ready for conditions, Context, tokens and ECA.

Use it when your host or CDN doesn't send a country header, or when you'd rather not depend on one. Lookups happen on your server: no request goes to an outside service.

Never use it for access control

A country from an IP address is a guess. VPNs, proxies and mobile networks move visitors to other countries. Use the country to choose what content to show, never to grant or refuse access.

Privacy

  • Consent comes first. Visitor Attributes looks up the country only for visitors whose consent allows it, the first time a page reads it.
  • Only the country is kept. It's stored in the visitor's Visitor Attributes cookie, so each visitor is looked up about once. The IP address is not stored, logged, cached or put in the cookie, and it's kept out of error messages.
  • The database stays private. The module refuses a file in public:// or anywhere inside the web root, including through a symlink, so the database can't be downloaded.

Features

  • Works with GeoLite2 and GeoIP2 Country, City and Enterprise databases, and with other databases in MaxMind's .mmdb format, such as DB-IP's free "lite" databases.
  • Country and continent; Visitor Attributes derives EU visitor from the country.
  • The file path can be absolute or a stream wrapper URI, such as private://geoip/GeoLite2-Country.mmdb. The settings form checks that the file opens and contains countries.
  • Addresses the database doesn't know, such as private and reserved ranges, simply give no country. IPv6 visitors with an IPv4-only database do too.
  • A missing or broken file shows on the status report, and lookups pause for a minute instead of failing on every request.

Requirements

  • Drupal 11.3 or later, or Drupal 12.
  • Visitor Attributes 1.0.
  • The geoip2/geoip2 library, version 3. Composer installs it with this module.
  • A database file with countries. A Country database is enough. GeoLite2 needs a free MaxMind account and a license key; read MaxMind's license, which limits use and requires updates.

Getting started

  1. composer require drupal/visitor_attributes_geoip, then install the module.
  2. Download a database and put it outside the web root, for example in the private files directory.
  3. At Configuration › System › Visitor attributes, under "Country", choose "MaxMind database (GeoLite2 or GeoIP2)" and save. Then enter the file's path and save again.
  4. Check the status report: the "Visitor Attributes country" entry shows any problem with the file.

This module doesn't download or update the database. MaxMind's geoipupdate tool, run from cron, can do both.

Performance

A lookup is cheap: on a development machine, with DB-IP's 8.5 MB Country Lite database and geoip2's pure-PHP reader, opening the file and looking up one address took about 0.21 ms, and a lookup with the file already open about 0.04 ms. A Drupal database cache read took about 0.08 ms. Results therefore aren't cached. The maxminddb PHP extension makes lookups faster still.

Project information

Releases