Get Geolocation from IP Address using PHP

IP Geolocation helps to know the location of a visitor by IP address. Generally, the IP address is used to get the location of visitors with geolocation information. To get the geolocation, we need to detect the IP address first. Based on the IP address, the location information (country, state, city, etc.) can be retrieved. There are various Third-Party API services available to collect geolocation data from IP addresses. But you can use PHP to get the IP geolocation of visitors without any 3rd Party web service. In this tutorial, we will show you how to detect the user’s IP address and get geolocation from IP using PHP.

The $_SERVER variable is an easy option to get the user’s IP address in PHP. Once the IP address is collected, we will use the GeoIP2 database to get the location of IP address using PHP. Furthermore, the GeoIp2 library helps to retrieve the geolocation data from the binary Database with PHP.

In this example script, we will do the following operations without depending on any web service or API using PHP.

  • Get the IP address of the current user.
  • Get geolocation information from the IP address using PHP.
  • Detect the visitor’s country, state (regions), city, and postal code associated with their IPv4 and IPv6 addresses.

Get the IP Address of the Current User

Use the REMOTE_ADDR index of $_SERVER to get the current user’s IP address in PHP.

$ip_addr $_SERVER["REMOTE_ADDR"];

If you want to get location from a specific IP address, specify it in the $ip_addr variable.

$ip_addr '154.47.26.248'; 

Get Geolocation from IP Address using PHP

GeoLite2 Geolocation Database:
Before getting started, you need to download the latest GeoLite2-City database from the MaxMind account.

  • Place this GeoLite2 database binary file (GeoLite2-City.mmdb) in your project directory.

Download & Install GeoIp2 Library:
The GeoIp2 library helps to retrieve the geolocation data from GeoLite2 binary database based on the IP address using PHP.

Run the following command to download and install the GeoIp2 PHP library via Composer.

composer require geoip2/geoip2

Note that: You don’t need to download this library separately, all the required files are included in our source code. You can install and access the GeoIp2 library without using Composer.

Define Database Reader Object:
To parse the GeoLite2 binary database, we need to create a GeoIp2\Database\Reader object from GeoIp2 class.

Include the autoloader and use the GeoIp2\Database\Reader namespace to create a Reader object.

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

Retrieve Geolocation data from GeoLite2 Database with PHP:

  • In the Reader object, pass the path of the database file as the first argument.
  • Specify the IP address in the first argument of the city() method.
  • The following geolocation data will be returned from the database based on the given IP address.
    • IP Address (traits->network)
    • Country Code (country->isoCode)
    • Country Name (country->name)
    • State Code (mostSpecificSubdivision->isoCode)
    • State Name (mostSpecificSubdivision->name)
    • City Name (city->name)
    • Zipcode (postal->code)
    • Latitude (location->latitude)
    • Longitude (location->longitude)
try { 
  
// Create a new Reader object and specify the path to the database file
  
$cityDbReader = new Reader('path/to/GeoLite2-City.mmdb');

  
// Get geolocation data
  
$record $cityDbReader->city($ip_addr);
} catch(
Exception $e) {
  
$api_error $e->getMessage();
}

if(empty(
$api_error)){
  
$ip_address = !empty($record->traits->network)?$record->traits->network:'';
  
$country_code = !empty($record->country->isoCode)?$record->country->isoCode:'';
  
$country_name = !empty($record->country->name)?$record->country->name:'';
  
$state_code = !empty($record->mostSpecificSubdivision->isoCode)?$record->mostSpecificSubdivision->isoCode:'';
  
$state_name = !empty($record->mostSpecificSubdivision->name)?$record->mostSpecificSubdivision->name:'';
  
$city_name = !empty($record->city->name)?$record->city->name:'';
  
$zipcode = !empty($record->postal->code)?$record->postal->code:'';
  
$latitude = !empty($record->location->latitude)?$record->location->latitude:'';
  
$longitude = !empty($record->location->longitude)?$record->location->longitude:'';

  echo 
'IP Address: '.$ip_address.'<br/>';
  echo 
'Country Code: '.$country_code.'<br/>';
  echo 
'Country Name: '.$country_name.'<br/>';
  echo 
'State Code: '.$state_code.'<br/>';
  echo 
'State Name: '.$state_name.'<br/>';
  echo 
'City Name: '.$city_name.'<br/>';
  echo 
'Zipcode: '.$zipcode.'<br/>';
  echo 
'Latitude: '.$latitude.'<br/>';
  echo 
'Longitude: '.$longitude;
}else{
  echo 
$api_error;
}

Get Location from IP Address using Geolocation API

Conclusion

The main advantage of using the GeoLite2 Database is you can get the location from an IP address free of cost. You don’t require to use any web service or API to get the geolocation of an IP address. Since the location data is fetched from the local database file, there is no 3rd-Party dependency to get Country State City Zipcode details of the IP address. This IP Geolocation script provides an easy way to get the location of IP address using PHP.

Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request

3 Comments

  1. Mallcom Said...
  2. Michael Said...
  3. Himanshu Tyagi Said...

Leave a reply

keyboard_double_arrow_up