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

Get Country, City, latitude, 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 IP address in PHP.

Here, we will use free API to get the user’s location information like country name, city name, state name, latitude, longitude etc.

Get Location Related Information from Ip Address PHP

Here, we will create a PHP script that is used to get the user’s city, state, and country, latitude, longitude, and timezone, etc.

Just follow the below steps, and get user’s or visitor’s location related information.

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:" . $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>


//static ip address
$ip = ""; 

//Get IP Address of User in PHP
//$ip = $_SERVER['REMOTE_ADDR']; 

//call api
$url = file_get_contents("" . $ip);

//decode json data
$getInfo = json_decode($url); 

//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>";


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

  1. Switch Statement in PHP | Decision-Making Statements
  2. PHP empty() – Check Variable is Empty in PHP
  3. isset() and unset() Function In PHP – Examples
  4. PHP Convert String to Array PHP
  5. jQuery AJAX Form Submit PHP MySQL
  6. PHP Sending Email (text/html)|PHP Mail() Function
  7. PHP Array: Indexed,Associative, Multidimensional
  8. PHP 7 File Upload | PHP Tutorial
  9. Create & Drop Database MySQL Using Command Line & PHP
  10. Connect MySQL Database to Command-Line OR PHP Script
  11. String functions in PHP – Strrev, strlen, implode, trim
  12. Array Functions In PHP – PHP Tutorial


My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Javascript, JQuery, Laravel, Livewire, Codeigniter, Vue JS, Angular JS, React Js, WordPress, and Bootstrap from a starting stage. As well as demo example.

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

  1. nice tuts.

Leave a Reply

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