
Displaying the nearest cities to a visitor can help deliver more relevant local information, such as weather forecasts, news, events, and nearby businesses. This is especially useful for travelers and users searching for services and amenities in surrounding areas.
In this tutorial, you’ll learn how to identify a visitor’s current location and retrieve the nearest city or cities based on their IP address using PHP, MySQL, and the GeoDataSource™ Location Search Web Service.
The process involves two steps:
Nearest City Search – Use the latitude and longitude returned from the IP lookup to query the GeoDataSource™ Location Search Web Service and obtain nearby city names.
IP Geolocation Lookup – Use the IP2Location™ LITE database to determine the visitor’s latitude and longitude from their IP address.
Accurate location data is a key component of many modern web applications. To get started, we recommend using the IP2Location DB11 LITE database, which includes country, region, city, latitude, longitude, ZIP/postal code, and time zone information. Best of all, it is available free of charge. Download the database, extract the data file, and import it into your MySQL database from https://www.ip2location.com/database/lite
For finding nearby cities, this tutorial uses the GeoDataSource™ Location Search Web Service, which is also available for free. Before using the service, you will need to create an account and obtain an API key.
Follow the steps below to configure the database, perform the IP geolocation lookup, and retrieve the nearest city information based on the visitor’s location.
Step 1: Create ‘ip2location_db11’ table.
CREATE DATABASE ip2location; USE ip2location; CREATE TABLE `ip2location_db11`( `ip_from` INT(10) UNSIGNED, `ip_to` INT(10) UNSIGNED, `country_code` CHAR(2), `country_name` VARCHAR(64), `region_name` VARCHAR(128), `city_name` VARCHAR(128), `latitude` DOUBLE, `longitude` DOUBLE, `zip_code` VARCHAR(30), `time_zone` VARCHAR(8), INDEX `idx_ip_from` (`ip_from`), INDEX `idx_ip_to` (`ip_to`), INDEX `idx_ip_from_to` (`ip_from`, `ip_to`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
Step 2: Import the data into table ‘ip2location_db11’.
LOAD DATA LOCAL INFILE 'IP2LOCATION-LITE-DB11.CSV' INTO TABLE `ip2location_db11` FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY 'rn' IGNORE 0 LINES;
Step 3: Sign up and download GeoDataSourceâ„¢ Location Search Web Service to get your API key.
Step 4: Create php file nearestcity.php and run the sample codes.
<!DOCTYPE html>
<head>
<title>Nearest City / Cities</title>
</head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<!--Toggle button to show and hide the output-->
<script>
$(document).ready(function() {
$("#b2").click(function() {
$("#cities").toggle();
});
});
</script>
<body>
<!--Let visitor enters their IP address -->
<form action="nearestcity.php" method="post">
IP Address: <input type="text" name="ipaddress"><br>
<input type="submit" name="submit">
</form>
<div>
<?php
//MySQL server configuration
$server = "host";
$username = "username";
$password = "password";
$database = "ip2location";
//Connect to database server
$conn = mysqli_connect($server, $username, $password, $database) or die("Could not connect to MySQL database");
//Function to convert IP address (xxx.xxx.xxx.xxx) to IP number (0 to 256^4-1)
function Dot2LongIP ($IPaddr) {
if ($IPaddr == ""){
return 0;
}
else {
$ips = explode(".", $IPaddr);
return ($ips[3] + $ips[2] * 256 + $ips[1] *256 * 256 + $ips[0] * 256 * 256 * 256);
}
}
if(isset($_POST['submit'])){
//Retrieve visitor IP address
$ipaddress = $_POST["ipaddress"];
//Convert IP address to IP number for querying database
$ipnum = Dot2LongIP ($ipaddress);
//SQL query string to match the recordset that the IP number fall between the valid range
$query = "SELECT * FROM ip2location_db11 WHERE $ipnum <= ip_to LIMIT 1";
//Execute SQL query
$result = mysqli_query($conn, $query) or die("IP2Location Query Failed");
while($row = mysqli_fetch_assoc($result)){
echo "<h3>Your current location: </h3>";
echo "Country Code: " . $row['country_code'] . "<br>";
echo "Country Name: " . $row['country_name'] . "<br>";
echo "Region: " . $row['region_name'] . "<br>";
echo "City: " . $row['city_name'] . "<br>";
echo "Latitude: " . $row['latitude'] . "<br>";
echo "Longitude: " . $row['longitude'] . "<br>";
echo "Zip Code: " . $row['zip_code'] . "<br>";
echo "Time Zone: " . $row['time_zone'] . "<br>";
//Get the data of city & cities
$apiKey = 'ENTER_YOUR_KEY';
$params['format'] = 'json';
$params['lat'] = $row['latitude'];
$params['lng'] = $row['longitude'];
$query2 = '';
foreach($params as $key=>$value){
$query2 .= '&' . $key . '=' . rawurlencode($value);
}
$rawJson = file_get_contents('https://api.geodatasource.com/city?key=' . $apiKey . $query2);
$datas = array(json_decode ($rawJson));
$rawJson2 = file_get_contents('https://api.geodatasource.com/cities?key=' . $apiKey . $query2);
$datas2 = json_decode ($rawJson2);
//One city
echo "<h3>Nearest City</h3>";
foreach($datas as $data):
echo "<li>" . $data->city . "</li>";
endforeach;
//More cities
echo "<h3>Nearest Cities</h3>";
echo '<button id="b2" onClick="cities()">Your nearest cities</button>';
}
}
?>
<p id="cities" style="display:none"></p>
<script>
function cities(){
document.getElementById("cities").innerHTML
= "<?php
foreach($datas2 as $data2):
echo "<li>" . $data2->city . "</li><br>";
endforeach;
?>" ;
}
</script>
</body>
</html>
