Skip to content

How to Get User Current Location ?

  • by
get user location

In this article you will learn how to get visitor’s current location. For this, we are using the HTML5 Geolocation API. HTML5 Geolocation API is used to get 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 click on the allow button to share their location.

Detecting Your Location With PHP

get user locationMost modern devices are capable of detecting their own location either through GPS, WiFi, or IP geolocation. Developers can use this information to provide better search suggestions, nearby store locations, and implement all kinds of useful map interactions in their apps and websites.

// Find Location

function get_client_location_ip() {
    $ipaddress = '';
    if (isset($_SERVER['HTTP_CLIENT_IP']))
    $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
    else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
    $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
    else if(isset($_SERVER['HTTP_X_FORWARDED']))
    $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
    else if(isset($_SERVER['HTTP_FORWARDED_FOR']))
    $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
    else if(isset($_SERVER['HTTP_FORWARDED']))
    $ipaddress = $_SERVER['HTTP_FORWARDED'];
    else if(isset($_SERVER['REMOTE_ADDR']))
    $ipaddress = $_SERVER['REMOTE_ADDR'];
    $ipaddress = 'UNKNOWN';
    return $ipaddress;
 $PublicIP = get_client_location_ip(); 
$json  = file_get_contents("$PublicIP/geo");
$json  =  json_decode($json ,true);

 $country = $json['country'];
 $region  = $json['region'];
 $city    = $json['city'];
 $postal  = $json['postal'];

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face – we are here to solve your problems.

Leave a Reply

Your email address will not be published. Required fields are marked *