Updated: 28 September 2026 · Applies to: PHP 8.1 and newer (MaxMind GeoIP2 / GeoLite2)
The old PHP geoip extension and the legacy GeoIP databases it read are discontinued: MaxMind stopped updating those databases in 2019. Use the GeoIP2 / GeoLite2 databases instead.
1. Get the database
- Create a free MaxMind account, generate a licence key, and download GeoLite2-Country or GeoLite2-City (
.mmdbfile). - Keep it updated with MaxMind's
geoipupdatetool (dnf install geoipupdate/apt install geoipupdate) and a weekly cron job.
2. Read it from PHP
The simplest way, which also works on shared hosting, is MaxMind's Composer library:
composer require geoip2/geoip2
<?php
require 'vendor/autoload.php';
$reader = new GeoIp2\Database\Reader('/home/user/geo/GeoLite2-Country.mmdb');
$record = $reader->country($_SERVER['REMOTE_ADDR']);
echo $record->country->isoCode; // e.g. IN
For more speed on a VPS, also install the C extension maxminddb from PECL; the library uses it automatically.
Tip: If your site is behind Cloudflare, the visitor's country is already sent in the CF-IPCountry header, so no database is needed for country checks.