Get current location address in php

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:

Then create another php file name 'get_location.php' and copy and paste the given code.

2. get_location.php



In the above code, first we request the Geocoding API in the following format, which returns the output in JSON format.

//maps.googleapis.com/maps/api/geocode/json?latlng='.trim[$_POST['latitude']].','.trim[$_POST['longitude']].'&sensor=false';

Then, we have used the file_get_contents[] to read the contents of the JSON file as a string and decode this using json_decode[] function.

$json = @file_get_contents[$url];
$data = json_decode[$json];

And finally, we have received the current visitor's location address in the following variable.

$location = $data->results[0]->formatted_address;

Related Articles

How can I find a visitors location?

An IP address lookup is another way to get the location of your visitor. We usually use public IP lookup services/APIs for this option. Some of these are paid services and some are free. Examples include IP Geolocation API, IPinfo, and GEOIP DB.

How do I get my current location on Google Maps?

Get someone's location.
On your Android phone or tablet, open the Google Maps app​ ..
Tap your profile picture or initial. Location sharing..
Tap the profile of the person you want to find. To update the person's location: Tap on a friend's icon More. Refresh..

How can I get latitude and longitude from address in PHP?

function getaddress[$lat, $lng] { $url = '//maps.googleapis.com/maps/api/geocode/json?latlng=' . trim[$lat] . ',' .

How do you get geolocation without permission?

You can go for an external service like //www.geolocation-db.com. They provide a geolocation service based on IP addresses where you don't need user permission.

Chủ Đề