27

Possible Duplicate:
Location detecting techniques for IP addresses

For our website it's important to know from exactly which country our visitor is coming from.

I guess the best answer for my question would be the simple geo location feature of current browser systems, which just ask the user if the website is allowed to see his geographic location: http://dev.w3.org/geo/api/spec-source.html

but I don't want to bother visitor with questions, I would love to automatically detects the visitor's location (country should be enough.)

What's the best way to do this? - what ip database would be the best? - is the browser header trustable enough to detect his country? (en-us en-en en-ca?) What's the best solution?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Lupo
  • 2,884
  • 5
  • 24
  • 30
  • 6
    Language ≠ geographic location. – Gumbo Nov 14 '10 at 18:44
  • 2
    This is the highest ranked by Google. It has been better asked and better title than the question of which this is a supposed duplicate. – 3Dom Jan 22 '15 at 10:56
  • Maybe using the PECL extension Geo IP Location http://php.net/manual/en/book.geoip.php – TimD Nov 04 '15 at 10:48
  • If you use IP Address location, you don't need user permission to get his/her location. But this will be inaccurate if he/she use VPN or proxy. However if you want accuracy, you need to use Geolocation. But user will know that you are tracking his/her location. So far there are no method to obtain user's precise location without his/her consent. – 4 Leave Cover Nov 11 '16 at 05:59

4 Answers4

16

NetImpact provide a free API for geolocation lookup by IP, ProgrammableWeb also has a roundup of GeoIP lookup providers. This involves a small amount of latency while your application completes an API call (unless your application can use a non-blocking call) but is the least intrusive manner of detecting a visitor's country of origin.

Andy
  • 17,423
  • 9
  • 52
  • 69
11

The browser header just tells you what language the user wants to see. A Spanish-speaking person on a business trip to Japan probably wants to see websites in Spanish.

I think you can use reverse-IP services to make a guess, but if it's for commerce purposes I think you have to ask the user to tell you.

Similarly with geo-location: the physical position of the computer may or may not be relevant to the needs of the user browsing the site.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • A Spanish in Japan is an exception. Making a site, even commerce, by default Japanese with an option on screen for change, will fit the majority of visitors. – Mic Nov 14 '10 at 18:58
  • 4
    If the browser sends you an `Accept-Language` header that indicates the user wants Castillian Spanish first, why would you override that (if you've got that locale available anyway)? – Pointy Nov 14 '10 at 19:59
  • A Spanish speaker in Japan is NOT an exception. People travel. Ignoring the language settings (=what the user wants) and pushing a language into his face is very annoying if not offensive. – Steven M Oct 08 '16 at 08:53
1

Have a look at this page on the Google Maps API: http://code.google.com/apis/maps/documentation/javascript/basics.html#DetectingUserLocation

Drew
  • 116
  • 4
0

http://www.iplocationtools.com/samples.html also presents a good set of apis to do this. I have used their system before and they were quite reliable.

relima
  • 3,462
  • 5
  • 34
  • 53