Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get user's current location

How can I determine user's current location based on IP (I guess it works this way).

like image 222
Jacek Francuz Avatar asked Mar 22 '11 22:03

Jacek Francuz


People also ask

What API is used to locate a user's position?

The HTML Geolocation API is used to locate a user's position.

How do I find my current location API?

To obtain the user's current location, you can call the getCurrentPosition() method. This initiates an asynchronous request to detect the user's position, and queries the positioning hardware to get up-to-date information. When the position is determined, the defined callback function is executed.

What does watchPosition () is used for?

The Geolocation method watchPosition() method is used to register a handler function that will be called automatically each time the position of the device changes.


2 Answers

<?php $user_ip = getenv('REMOTE_ADDR'); $geo = unserialize(file_get_contents("http://www.geoplugin.net/php.gp?ip=$user_ip")); $country = $geo["geoplugin_countryName"]; $city = $geo["geoplugin_city"]; ?> 
like image 113
Hamza Avatar answered Nov 01 '22 14:11

Hamza


Edited

Change freegeoip.net into ipinfo.io

<?php      function get_client_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'];     } else {         $ipaddress = 'UNKNOWN';     }      return $ipaddress; } $PublicIP = get_client_ip(); $json     = file_get_contents("http://ipinfo.io/$PublicIP/geo"); $json     = json_decode($json, true); $country  = $json['country']; $region   = $json['region']; $city     = $json['city']; ?> 
like image 32
Naveen DA Avatar answered Nov 01 '22 14:11

Naveen DA