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

In this tutorial, you will learn how to get location, country name, city name, state or region name, latitude, longitude, country code, time zone, etc, from user or visitor IP address in 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)

How to Get Location(Country, city Name), Latitude, Longitude from IP address in PHP

Use the following steps to get location, country name, city name, state or region name, latitude, longitude, country code, time zone, etc, from IP address in PHP:

  • Step 1 – Include API in the PHP File
  • Step 2 – Get Geolocation of User By IP Address

Step 1 – Include API in PHP File

Include geolocation API in your index.php file, and pass the user IP address in it:

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

Step 2 – Get Geolocation of User By IP Address

Call geolocation API and fetch user country name, city name, state or region name, latitude, longitude, country code, time zone, etc, from IP address:

<!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.

Recommended PHP Tutorials

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

One reply to Get Country, City Name, latitude, and longitude from IP address using PHP

  1. nice tuts.

Leave a Reply

Your email address will not be published. Required fields are marked *