What is the best way to get a site visitor's location?

B Seven picture B Seven · Dec 19, 2011 · Viewed 17.3k times · Source

After googling, I found many solutions that don't seem to work or require permission from the user.

Looking for something that is easy to use, PHP or Javascript, and does not require the user's permission. It doesn't need to be the most accurate.

Answer

Vigrond picture Vigrond · Dec 19, 2011

Google Analytics is great for this (javascript).

EDIT: Should point out that Google Analytics has an API to get City/Country info and all reporting info if you need to programmatically do something with it - http://code.google.com/apis/analytics/docs/

But if that's not an option:

You can get a users ip with PHP's $_SERVER['REMOTE_ADDR']

With that, you can use PHP's GeoIP library to geolocate it: http://php.net/manual/en/book.geoip.php