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.
Use the REMOTE_ADDR index of $_SERVER to get the current user’s IP address in PHP.
$userIP = $_SERVER['REMOTE_ADDR'];
Use the IP Geolocation API to get the user’s location from IP using PHP.
There are various info is available about geolocation in API response. Some of the most useful location details are:
country_name
)country_code
)region_code
)region_name
)city
)zip_code
)latitude
)longitude
)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($ch, CURLOPT_RETURNTRANSFER, true);
// Execute API request
$apiResponse = curl_exec($ch);
// Close cURL resource
curl_close($ch);
// Retrieve IP data from API response
$ipData = json_decode($apiResponse, true);
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.
<?php
function IPtoLocation($ip){
$apiURL = 'https://freegeoip.app/json/'.$ip;
// Make HTTP GET request using cURL
$ch = curl_init($apiURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$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($apiResponse, true);
// 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
Can this work on local sever
Nice and good thank you
Brilliant!
nice tutorial