How to Install the PHP GeoIP Extension

User Avatar
👤 admin
🔴 Admin
✍️ The most important thing in the world is to not be alone.
⏳ Last active: 15 Apr 2025 at 16:01
📅 Created: 14 Feb 2021 at 12:22
👀 Viewed: 18 times
✉️ Send Email

The GeoIP extension allows you to find the location of an IP address. Like the city, state, country, latitude, and longitude.

If you'd like to use the new GeoIP2, MaxMind now offers a pure PHP implementation that doesn't require a C extension: https://github.com/maxmind/GeoIP2-php.


sudo wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz

sudo gunzip GeoLiteCity.dat.gz

sudo mkdir -v /usr/share/GeoIP

sudo mv -v GeoLiteCity.dat /usr/share/GeoIP/GeoIPCity.dat

sudo apt-get install php-geoip

And here is an example how to use it with PHP7:


$ip = $_SERVER['REMOTE_ADDR'];

$country = geoip_country_name_by_name($ip);

echo 'The current user is located in: ' . $country;
If you want to comment: Login or Register