Get Geolocation (Country, Latitude, and Longitude) from IP Address using PHP

Geolocation provides information about the geographic location of a user. Specifically, the IP address is used by the geolocation service to determine the location. To track the visitor’s location, the first thing needed is an IP address. Based on the IP address, we can collect the geolocation info of the visitor. The PHP $_SERVER variable is the easiest way to get the user’s IP address. Based on the visitor’s IP address, you can detect the location with latitude and longitude using PHP. In this tutorial, we will show you how to get location from IP address using PHP.

The Geolocation API is an instant way to find the location of a user by IP address. You can use a free Geolocation API in PHP to fetch location information from an IP address. This example script will use IP Geolocation API to get location, country, region, city, latitude, and longitude from IP address using PHP.

Get IP Address of User with PHP

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

$userIP $_SERVER['REMOTE_ADDR'];

Get Location from IP Address using PHP

Use the IP Geolocation API to get the user’s location from IP using PHP.

  • Call API via HTTP GET request using cURL in PHP.
  • Convert API JSON response to array using json_decode() function.
  • Retrieve IP data from API response.

There are various info is available about geolocation in API response. Some of the most useful location details are:

  • Country Name (country_name)
  • Country Code (country_code)
  • Region Code (region_code)
  • Region Name (region_name)
  • City (city)
  • Zip Code (zip_code)
  • Latitude (latitude)
  • Longitude (longitude)
  • Time Zone (time_zone)
<?php 

// IP address
$userIP '162.222.198.75';

// API end URL
$apiURL 'https://freegeoip.app/json/'.$userIP;

// Create a new cURL resource with URL
$ch curl_init($apiURL);

// Return response instead of outputting
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);

// Execute API request
$apiResponse curl_exec($ch);

// Close cURL resource
curl_close($ch);

// Retrieve IP data from API response
$ipData json_decode($apiResponsetrue);

if(!empty(
$ipData)){
    
$country_code $ipData['country_code'];
    
$country_name $ipData['country_name'];
    
$region_code $ipData['region_code'];
    
$region_name $ipData['region_name'];
    
$city $ipData['city'];
    
$zip_code $ipData['zip_code'];
    
$latitude $ipData['latitude'];
    
$longitude $ipData['longitude'];
    
$time_zone $ipData['time_zone'];
    
    echo 
'Country Name: '.$country_name.'<br/>';
    echo 
'Country Code: '.$country_code.'<br/>';
    echo 
'Region Code: '.$region_code.'<br/>';
    echo 
'Region Name: '.$region_name.'<br/>';
    echo 
'City: '.$city.'<br/>';
    echo 
'Zipcode: '.$zip_code.'<br/>';
    echo 
'Latitude: '.$latitude.'<br/>';
    echo 
'Longitude: '.$longitude.'<br/>';
    echo 
'Time Zone: '.$time_zone;
}else{
    echo 
'IP data is not found!';
}

?>

For better usability, you can group all the code in a function.

  • The following IPtoLocation() function returns geolocation data from IP address.
<?php 
function IPtoLocation($ip){
    
$apiURL 'https://freegeoip.app/json/'.$ip;
    
    
// Make HTTP GET request using cURL
    
$ch curl_init($apiURL);
    
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
    
$apiResponse curl_exec($ch);
    if(
$apiResponse === FALSE) {
        
$msg curl_error($ch);
        
curl_close($ch);
        return 
false;
    }
    
curl_close($ch);
    
    
// Retrieve IP data from API response
    
$ipData json_decode($apiResponsetrue);
    
    
// Return geolocation data
    
return !empty($ipData)?$ipData:false;
}

Call IPtoLocation() and pass the IP address in the first argument.

$userIP '162.222.198.75'; 
$locationInfo IPtoLocation($userIP);

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

4 Comments

  1. Augustine Said...
  2. Aldona Said...
  3. Ligma Balls Said...
  4. Davis Makori Said...

Leave a reply

keyboard_double_arrow_up