Get Country, City, latitude, longitude from IP address using PHP

Get Country, City, latitude, 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.

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)

Get Location Related Information from Ip Address in PHP

To get the user’s city, state, and country, latitude, longitude, and timezone, etc using ip address in PHP:

  • Step 1 – Include API in PHP File
  • Step 2 – Create Index.php File

Step 1 – Include API in PHP File

Include this API in your PHP file. Also, you need to pass the IP address in it.

You can see the following:

http://www.geoplugin.net/json.gp?ip=" . $ip

Step 2 – Create Index.php File

Next step, you need to create one PHP file and update the below code into your index.php file:


<!DOCTYPE html>
<html>
<body>
 
 
<?php
 
//static ip address
$ip = "52.25.109.230"; 
 
//Get IP Address of User in PHP
//$ip = $_SERVER['REMOTE_ADDR']; 
 
//call api
$url = file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $ip);
 
//decode json data
$getInfo = json_decode($url); 
 
print_r($getInfo);
     
//print the array to see the fields if you wish.
 
echo "<table border='1' width='50%' align='center'><tr><td>COUNTRY:</td><td>";
echo $getInfo->geoplugin_countryName;
echo "</td></tr><tr><td>CITY:</td><td>";
echo $getInfo->geoplugin_city;
echo "</td></tr><tr><td>STATE OR REGION:</td><td>";
echo $getInfo->geoplugin_region;
echo "</td></tr><tr><td>IP ADDRESS:</td><td>";
echo $getInfo->geoplugin_request;
echo "</td></tr><tr><td>COUNTRY CODE:</td><td>";
echo $getInfo->geoplugin_countryCode;
echo "</td></tr><tr><td>LATITUTE:</td><td>";
echo $getInfo->geoplugin_latitude;
echo "</td></tr><tr><td>LONGITUDE:</td><td>";
echo $getInfo->geoplugin_longitude;
echo "</td></tr><tr><td>TIMEZONE:</td><td>";
echo $getInfo->geoplugin_timezone;
echo "</td></tr><tr></table>";
     
?>
 
</body>
</html>

The above PHP script call the API with IP address and returns the user’s City, State, Country, Country code, Latitude, Longitude, timezone, etc.

Leave a Reply

Your email address will not be published.