0

I am looking for a way to find the nearest town/city and the county of the person browsing my website. Then I would like it to be something like:

$town = "London, England";

Or something like:

$town = "Bacup, Lancashire GB";

Google has managed to do something like this. When you search something, at the bottom of the page it says "Your village - from your internet address".

Thanks

  • Why? What's your reason? – GeorgeWL Mar 29 '16 at 11:31
  • What you're most likely after is something called "[geolocation](https://en.wikipedia.org/wiki/Geolocation)" Gogle has an API that will do it for you. There's snippets and libraries ready made for php, html and jquery if thats what you mean http://stackoverflow.com/questions/4724971/is-there-a-good-php-geolocation-service may be relevant http://www.w3schools.com/html/html5_geolocation.asp http://html5demos.com/geo https://developers.google.com/maps/documentation/javascript/examples/map-geolocation beware: modern browsers will ask for user permission before trying to get geolocation. – GeorgeWL Mar 29 '16 at 11:39
  • Yeah, that will be fine if it needs permission. If the user is browsing on a mobile/tablet device that has GPS, does it automatically use that? @TheGeorgeL – CoolCodeGuy Mar 29 '16 at 11:41
  • it will ask for permission on mobile too. which in most cases will freak the user out about privacy and make them close the tab. – GeorgeWL Mar 29 '16 at 11:42
  • what you doing it for anyhow? – GeorgeWL Mar 29 '16 at 12:26

2 Answers2

1

Since you want to know the location of the person viewing your website, the data will be taken from the client machine. Use

navigator.geolocation.getCurrentPosition(showPosition);

for getting the client's location. The showPosition is a function name to be passed, so define a function with this name. If you just want to show the location only on client machine, do as follows:

function showPosition(position) {
    var x = document.getElementById("demo");
    x.innerHTML = "Latitude: " + position.coords.latitude + 
    "<br>Longitude: " + position.coords.longitude;  
}

Or you can send data to your server too.

You can get a little more information about this at https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition

Now you have the coordinates ( find out, how accurate? ), use google map location services api to get the location name. The Google Maps Geolocation API - https://developers.google.com/maps/documentation/geolocation/intro

  • For Chrome 5.0+, IE 9.0+, Firefox 3.5+, Safari 5.0+ and Opera 16.0+ so make sure you put in validation, something like: `if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(success); } else { error('Geo Location is not supported'); }` – GeorgeWL Mar 29 '16 at 12:05
1

Get Geo-IP Information

Requests a geo-IP-server (netip.de) to check, returns where an IP is located (host, state, country, town).

<?php
   $ip='94.219.40.96';
   print_r(geoCheckIP($ip));
   //Array ( [domain] => dslb-094-219-040-096.pools.arcor-ip.net [country] => DE - Germany [state] => Hessen [town] => Erzhausen )

   //Get an array with geoip-infodata
   function geoCheckIP($ip)
   {
           //check, if the provided ip is valid
           if(!filter_var($ip, FILTER_VALIDATE_IP))
           {
                   throw new InvalidArgumentException("IP is not valid");
           }

           //contact ip-server
           $response=@file_get_contents('http://www.netip.de/search?query='.$ip);
           if (empty($response))
           {
                   throw new InvalidArgumentException("Error contacting Geo-IP-Server");
           }

           //Array containing all regex-patterns necessary to extract ip-geoinfo from page
           $patterns=array();
           $patterns["domain"] = '#Domain: (.*?)&nbsp;#i';
           $patterns["country"] = '#Country: (.*?)&nbsp;#i';
           $patterns["state"] = '#State/Region: (.*?)<br#i';
           $patterns["town"] = '#City: (.*?)<br#i';

           //Array where results will be stored
           $ipInfo=array();

           //check response from ipserver for above patterns
           foreach ($patterns as $key => $pattern)
           {
                   //store the result in array
                   $ipInfo[$key] = preg_match($pattern,$response,$value) && !empty($value[1]) ? $value[1] : 'not found';
           }

           return $ipInfo;
   }

?>
Avinash Sinha
  • 534
  • 3
  • 16