In this tutorial I want to get information like city, state and country of the visitor from their IP address so I can modify my website to their location. Is there an efficient and reliable way to do this in PHP? Here I am using PHP to get geolocation of IP address in PHP.
See also
How to get Geolocation using PHP-cURL from IP Address
What is IP Geolocation?
IP Geolocation is a mechanism for mapping the virtual identity of an internet-connected device to its physical address. A virtual identity is represented by an Internet Protocol (IP) address. The physical address is the location of this device within the geographic area of the earth, represented by latitude and longitude. It can still be extrapolated to a geopolitical or postal address that we use on a daily basis to identify a place.
How to Get Geolocation with Country by IP Address in PHP
In this blog post there are two way to get geolocation with country by IP address using PHP.
- Get Geolocation from IP Address using PHP
- Get Geolocation without IP Address using PHP

1. Get Geolocation from IP Address using PHP
In this method you can get geolocation from user IP address basis to get the user country, city, zipcode, latitude and longitude.
Use API to get visitor geolocation: Here we are using https://ipinfo.io/ API to get visitor details. The API provides a JSON object that can be converted into a PHP variable.
The following code gets the location from IP address using PHP cURL.
index.php
<!DOCTYPE html> <html> <head> <title>Get Geolocation from IP Address using PHP</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <div class="container mt-5"> <h2 class="text-success text-center">Get Geolocation from IP Address using PHP</h2> <p class="text-success text-center">Prepare API request to get geolocation via PHP cURL</p> <table class="table table-striped"> <thead> <tr> <th>Country</th> <th>Region</th> <th>City</th> <th>Zip</th> <th>Latitude</th> <th>Longitude</th> <th>IP Address</th> </tr> </thead> <?php // IP address $ip = $_SERVER['REMOTE_ADDR']; // Get the user's IP address // Create a new cURL resource with URL $ch = curl_init(); // API end URL curl_setopt($ch, CURLOPT_URL, "http://ipinfo.io/{$ip}/json"); // Return response instead of outputting curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Execute API request $execute = curl_exec($ch); // Close cURL resource curl_close($ch); // Retrieve IP data from API response $ipResult = json_decode($execute); if (!empty($ipResult)) { ?> <tbody> <tr> <td><?php echo $ipResult->country ?></td> <td><?php echo $ipResult->region ?></td> <td><?php echo $ipResult->city ?></td> <td><?php echo $ipResult->postal ?></td> <?php $latLonArr = explode(",", $ipResult->loc); ?> <td><?php echo $latLonArr[0] ?></td> <td><?php echo $latLonArr[1] ?></td> <td><?php echo $ipResult->ip ?></td> </tr> </tbody> <?php }else{ echo 'IP data is not found!'; } ?> </table> </div> </body> </html>
2. Get Geolocation without IP Address using PHP
In this example you will learn How to Get Geolocation without IP address. but you can get the user location details like country, city, zipcode, latitude and longitude.
Use API to get visitor location details: Here we are using http://ip-api.com/json API to get visitor details. The API provides a JSON object that can be converted into a PHP variable.
The following code gets the location without IP address using PHP cURL.
location.php
<!DOCTYPE html> <html> <head> <title>Get Geolocation without IP Address using PHP</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <div class="container mt-5"> <h2 class="text-primary text-center">Get Geolocation without IP Address using PHP</h2> <p class="text-primary text-center">Another way to get geolocation via PHP cURL</p> <table class="table table-striped"> <thead> <tr> <th>Country</th> <th>Region</th> <th>RegionName</th> <th>City</th> <th>Zip</th> <th>Latitude</th> <th>Longitude</th> <th>IP Address</th> </tr> </thead> <?php // Create a new cURL resource with URL $ch = curl_init(); // API end URL curl_setopt($ch, CURLOPT_URL, "http://ip-api.com/json"); // Return response instead of outputting curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Execute API request $result = curl_exec($ch); // Close cURL resource curl_close($ch); // Retrieve IP data from API response $result = json_decode($result); if ($result->status == 'success') { ?> <tbody> <tr> <td><?php echo $result->country ?></td> <td><?php echo $result->region ?></td> <td><?php echo $result->regionName ?></td> <td><?php echo $result->city ?></td> <td><?php echo $result->zip ?></td> <td><?php echo $result->lat ?></td> <td><?php echo $result->lon ?></td> <td><?php echo $result->query ?></td> </tr> </tbody> <?php } ?> </table> </div> </body> </html>
Frequently Asked Questions
-
How do you find geolocation?
On your computer, open Google Maps. Right-click the place or area on the map. This will open a pop-up window. You can find your latitude and longitude in decimal format at the top.
-
What does geolocation do?
Geolocation is the ability to determine an internet user’s physical location. This awareness can be used for almost any purpose, from targeted advertising and fraud prevention to law enforcement and emergency response.
-
What is the meaning of geolocation?
Geolocation refers to the use of location technologies such as GPS or IP addresses to identify and track the whereabouts of connected electronic devices. Because these devices are often carried on an individual’s person, geolocation is often used to track the movements and location of people and surveillance
-
How to get geolocation from IP address in PHP?
To get Geolocation information from an IP address in PHP, you can use various third-party APIs and services that provide Geolocation data based on IP addresses. A popular option is the IPinfo API, which offers a free tier for basic usage.
-
How to get geolocation using PHP?
To get geolocation data with PHP, you can use a combination of the HTML5 Geolocation API and third-party services. Here’s a simple example of using the HTML5 Geolocation API with the Leaflet library to display a location on a map:
-
How do I find geolocation from IP address?
To get Geolocation information from an IP address in PHP, you can use various third-party APIs and services that provide Geolocation data based on IP addresses. A popular option is the IPinfo API, which offers a free tier for basic usage.