How to geolocate an IP address in PHP

Last Updated Jan 08, 2021
Emma Jagger

Engineer, maker, Google alumna, CMU grad

Table of Contents:

IP-based geolocation is a way to physically locate a computer, a mobile, or any device connected to the Internet.

To get your visitors' physical location, you need two things: the IP address of your target, which you can obtain using a simple PHP script, and a geolocation search tool. The geolocation tool can query its database to returns details about the requested IP address's physical location. In order to identify yours, use our tool to see what is your IP address and location.

If you manage to retrieve his IP address, you can obtain the device's location within a few kilometers. The best geolocation tools will get you the following information: country, region, city, postal code, longitude, and latitude.

How to retrieve user's IP address in PHP

The PHP variable $_SERVER is an array whose entries are created by the webserver. It contains information about the server and the HTTP connection, such as HTTP headers and the client's IP address.

The easiest way to get the visitor's IP address is to use the REMOTE_ADDR entry, which returns the client's IP address requesting the current page.

echo `Client IP address: `.$_SERVER[`REMOTE_ADDR`];

But when the webserver is behind a proxy, REMOTE_ADDR will return the proxy's IP address, not the user's. In this case, the proxy would have set the real client's IP address in an HTTP header, generally HTTP_CLIENT_IP or HTTP_X_FORWARDED_FOR. Here is a script that covers most of the cases:

function clientIpAddress(){
    $address = $_SERVER['HTTP_CLIENT_IP'];
    $ address = $_SERVER['HTTP_X_FORWARDED_FOR'];
    $ address = $_SERVER['REMOTE_ADDR'];
  return $address;

echo 'Client IP address: '.clientIpAddress();

Don't reinvent the wheel.
Abstract's APIs are production-ready now.

Abstract's suite of API's are built to save you time. You don't need to be an expert in email validation, IP geolocation, etc. Just focus on writing code that's actually valuable for your app or business, and we'll handle the rest.

Get started for free

Using the free MaxMind IP geolocation tool in PHP

MaxMind offers multiple IP geolocation databases that can be downloaded for free from its website. The 2 main free databases are GeoLite2 Country that allows you to get the country name corresponding to an IP address, and GeoLite2 City to obtain country, region, and city name. The second being the most accurate but also the largest in terms of file size.

The first steps are to sign-up to the MaxMind website and validate your email address. Then you will be able to download the compressed databases from your account page. You will find the geolocation database in the compressed file, named GeoLite-Country.mmdb or GeoLite2-City.mmdb.

Then you will need to download the API used to query the MaxMind database. For PHP, the officially supported API can be found on GitHub, as well as its documentation.

Installation of GeoIP2 PHP API can be done through Composer. First, install Composer if it’s not already available on your system:

curl -sS | php

Then you can install the API and its dependencies:

php composer.phar require geoip2/geoip2:~2.0

To finally use this API, you must create a new \GeoIp2\Database\Reader object, passing the path to the MaxMind database file downloaded earlier, then you can query it. Here is an example of its usage with the City database:

require_once 'vendor/autoload.php';
use GeoIp2\Database\Reader;

// Creates the API reader object
$reader = new Reader('/path/to/GeoIP2-City.mmdb');

// Get information from the IP address
$address =  clientIpAddress();
$record = $reader->city($address);

echo($record->country->name); // 'United States'
echo($record->city->name); // 'Mountain View'
echo($record->location->latitude); // 37.4223
echo($record->location->longitude); // -122.085

Drawbacks of using MaxMind geolocation in PHP

Downloading the MaxMind database and installing its API are additional steps that may put off some of us. Since such a database is updated regularly, you will have to download it again on your servers each time it is updated.

Moreover, the MaxMind files being quite large, you may have a performance loss when several visitors are connected simultaneously on your server.

Alternative to MaxMind: the free Abstract API service

Abstract provides a free geolocation service based on your visitors' IP address, which is very simple to use, does not require installation of any additional components aside from libcurl, and always remain up-to-date without you having to worry about it.

You simply have to create an account on the Abstract website, which will generate your private API key automatically. Then a few lines of code are enough to get the geolocation of your visitors:

// Calling Abstract API endpoint using CURL library (
$api_key = 'your_private_api_key';
$address =  clientIpAddress();

$ch = curl_init(''.$api_key.'&ip_address='.$address);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);


Abstract's IP Geolocation API comes with PHP libraries, code snippets, guides, and more.

Get started for free
Abstract's IP Geolocation API comes with PHP libraries, code snippets, guides, and more.
Get started