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

  1. Create a free MaxMind account, generate a licence key, and download GeoLite2-Country or GeoLite2-City (.mmdb file).
  2. Keep it updated with MaxMind's geoipupdate tool (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.

Related services

KVM VPS hosting

NVMe SSD and full root access, with Free Basic Managed Support.

Managed cPanel VPS

cPanel included; we handle updates, security and monitoring.

Server management

on-demand administration, hardening, migrations and monitoring.

Was this answer helpful?

Still stuck? Open a ticket