Skip to Content
PHPCODE
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.

$userIP = $_SERVER['REMOTE_ADDR'];

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:

<?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!'; 
}
?>

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().

<?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; 
}

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

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