views:

265

answers:

4

I need a consistent and reliable way to determine the location (state/province, country, etc) of a computer that visits my website, using any of the following:

  • PHP
  • JavaScript
  • MySQL
  • Any Web Services
  • Something I haven't thought of?

I'm am not looking for a locked-out 3rd party stats program such as Google Analytics. It's already in use and does amazing stuff, however I need code level access to the information.

The ideal solution should allow me to provide whatever information is required (such as an ip address) and accurately obtain as much information as I can about the visit. Obtaining the country of origin is a minimum requirement however extras such as states, latitude and longitude, city, etc would be nice.

Clarification: I need to be able to use the information regularly in PHP code. Not just view it somewhere.

+6  A: 

It's called "geolocation", and it's been covered before.

Jim Puls
A: 

Hostip.info provides a free IP GeoLookup API for use in your application. However, IP lookups are NOT going to be consistent or reliable because of the ease of proxying. Anyone visiting your site from behind a proxy will likely be reported incorrectly.

However, it's the best you can do with an IP.

Andrew Flanagan
+1  A: 

If you mean 100% reliability, it is not there. But you can use MaxMind for geolocation, if reliable means less than 100%. They have an open source version:

http://www.maxmind.com/app/geolitecity

There is also a paid service, if you need more accuracy for your results.

Gregory A Beamer
A: 

Just for the country region. Geoip libraries are useful.

There's even a command line program in linux.

Also you can grab enviornment variables with your PHP script.

Here's a Perl script I wrote a while back to display the variables. Nothing fancy.

#!/usr/bin/perl

print 'Content-type: text/HTML' . "\n\n";
print "<body><center>";
print "<table border=1>";
print '<tr><td>' . $_ . "<td>" . $ENV{$_} . "<br>\n" foreach sort (keys(%ENV)) ;

You can see the browser

J.J.