Get Geolocation from IP address in PHP

Webs Codex
3 min readAug 16, 2023

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.

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.

  1. Get Geolocation from IP Address using PHP
  2. 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.

See also How to Delete record to MySql from PHP using jQuery AJAX

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.

See also How to Make CRUD REST API in PHP with MySQL

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>

If you want to learn for more details and blog posts visit to the

https://webscodex.com/

Get Geolocation from IP address in PHP

--

--