Skip to Content
get location from ip address javascript using php
oops code / September 9, 2021

Using geolocation, you can find out where a user is in relation to the world. The geolocation service, in particular, uses the IP address to determine the location of the user’s device. The IP address of the visitor is the first thing needed to track their location. We can determine the visitor’s location based on their IP address. An IP address can be obtained by using PHP’s $_SERVER variable. Based on the visitor’s IP address, PHP can determine the visitor’s latitude and longitude. Learn how to get a location from an IP address in this PHP tutorial.

By using the Geolocation API, you can find out a user’s location in real-time based on their IP address. It’s possible to get location information from an IP address using a free Geolocation API in PHP. IP Geolocation API is used in this example script in order to obtain the location, country, region, city, latitude, and longitude using PHP and the IP Geolocation API in order to do so.

Php code to get IP address of a user

To get the IP address of the current user in PHP, use the REMOTE ADDR index in $_SERVER.


Using PHP, find the location of an IP address

The IP Geolocation API in PHP can be used to determine a user’s location from their IP address.

The API response contains a variety of information about geolocation. Many useful details can be found here:

// IP address 
$userIP = ''; 
// API end URL 
$apiURL = ''.$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 
// Retrieve IP data from API response 
$ipData = json_decode($apiResponse, true); 
$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; 
echo 'IP data is not found!'; 

The code can be organised in a function to make it easier to use.

It returns geolocation data from IP address using the following function: IPtoLocation().

function IPtoLocation($ip){ 
$apiURL = ''.$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); 
return false; 
// Retrieve IP data from API response 
$ipData = json_decode($apiResponse, true); 
// Return geolocation data 
return !empty($ipData)?$ipData:false; 

Invoke IPtoLocation() with the IP address as the first argument.

$userIP = ''; 
$locationInfo = IPtoLocation($userIP);
PHPCODE © 2024