Solution : 1 The HTML5 geolocation API will get you the user's latitude and longitude (assuming they opt in). If you need ...
Solution : 1
The HTML5 geolocation API will get you the user's latitude and longitude (assuming they opt in). If you need to get info such as the city, country, or postal code, you'll need to use a reverse-geocoding web service. There are plenty of those out there:
So take your pick... just make sure to look at the terms & conditions and be sure you won't be in violation once the site goes live.
You should be able to find code samples by googling around a bit, it's a fairly common use case. The steps will be:
Get the user's lat/lng with your existing code.
Make an AJAX (JSONP) call to request the reverse-geocode (use your Geocode provider to figure out what URL to use. eg, for Google it is like this).
Parse the JSON response to extract the info you need (country, city).
Solution : 2
Try this code using the hostip.info service:
// Reformat the data returned (Keep only country and country abbr.)
$only_country=explode (" ", $country);
echo "Country : ".$only_country." ".substr($only_country,0,4);
Solution : 3
MaxMind GeoIP is a good service. They also have a free city-level lookup service.