The old freegeoip API is now deprecated and will be discontinued on July 1st, 2018.
The new API is from //ipstack.com. You have to create the account in ipstack.Then you can use the access key in the API url.
$url = "//api.ipstack.com/122.167.180.20?access_key=ACCESS_KEY&format=1";
$ch = curl_init[];
curl_setopt[$ch, CURLOPT_URL, $url];
curl_setopt[$ch, CURLOPT_RETURNTRANSFER, 1];
curl_setopt[$ch, CURLOPT_PROXYPORT, 3128];
curl_setopt[$ch, CURLOPT_SSL_VERIFYHOST, 0];
curl_setopt[$ch, CURLOPT_SSL_VERIFYPEER, 0];
$response = curl_exec[$ch];
curl_close[$ch];
$response = json_decode[$response];
$city = $response->city; //You can get all the details like longitude,latitude from the $response .
For more information check here :/ //github.com/apilayer/freegeoip
In this article, you will learn how to track the visitor's current location using the PHP programming language.
Geolocation in itself is an innovation that has saved, served and recommended incredible things, places to individuals all throughout the planet. By using geolocation, we can easily know how to go from one place to another. It can easily provide us with information, entertainment, security or weather updates.
For implementing this, we are using the HTML5 Geolocation API. The HTML5 Geolocation API is used to get the visitor's latitude and longitude and helps us to easily trace the visitor's full address [location, city, district, state, pincode], but this is possible only when the user clicks on the allow button of notification to share their current location. This notification is basically for privacy reasons.
Here is the simple code written in HTML5, PHP, and JavaScript to get the current visitor's location. The HTML5 Geolocation API is responsible for working in a secure context and providing visitors with their latitude and longitude. After that, we will call the Ajax file 'get_location.php' and pass the latitude and longitude to get the visitor's address.
So, let's first create a file name 'index.php' and call the jQuery library file in the head section as we have mentioned here. In the body section, we have created two JavaScript functions- getlocation[] and visitorLocation[]. The getlocation[] function is called when you click the 'Get Current Location' button and the visitorLocation[] function is called within it to get the latitude and longitude.
var lat = position.coords.latitude;
var long = position.coords.longitude;
After the above code, we have called the ajax file 'get_location.php' and passed the 'lat' and 'long' variables as parameters to the url.
1. index.php
Get Visitor's Current Location using PHP and JQuery
#container{ color: #116997; border: 2px solid #0b557b; border-radius: 5px;}
p{ font-size: 15px; font-weight: bold;}
function getlocation[] {
if [navigator.geolocation] {
if[document.getElementById['location'].innerHTML == ''] {
navigator.geolocation.getCurrentPosition[visitorLocation];
}
} else {
$['#location'].html['This browser does not support Geolocation Service.'];
}
}
function visitorLocation[position] {
var lat = position.coords.latitude;
var long = position.coords.longitude;
$.ajax[{
type:'POST',
url:'get_location.php',
data:'latitude='+lat+'&longitude='+long,
success:function[address]{
if[address]{
$["#location"].html[address];
}else{
$["#location"].html['Not Available'];
}
}
}];
}
Your Current Location: