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.
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';
GeoLite2 Geolocation Database:
Before getting started, you need to download the latest GeoLite2-City database from the MaxMind account.
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:
city()
method.traits->network
)country->isoCode
)country->name
)mostSpecificSubdivision->isoCode
)mostSpecificSubdivision->name
)city->name
)postal->code
)location->latitude
)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
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
Thanks. Works perfect in php 8.2 and nette framework !!
Nice video!
Do you have sample codes in YouTube to retrieve IP geolocation using API such as IP2Location.io instead of local database?
Geolocation in PHP is a fantastic way to personalize user experiences based on their location. By using APIs like MaxMind GeoIP or IPStack, you can retrieve a user’s geolocation data from their IP address. This includes their country, region, city, and latitude/longitude coordinates.