tags:

views:

1369

answers:

7

How to get location from ip address. I want to retrieve information like city, state, country of my visitors from their ip address, so that i can customize my web page. I am using php/mysql/javascript for server and client scripting. Is their a way to do the above.

+4  A: 

You need to use an external service... such as http://www.hostip.info/ if you google search for "geo-ip" you can get more results.

The Host-IP API is HTTP based so you can use it either in PHP or JavaScript depending on your needs.

null
I have been using hostip.info for a year now and I'm NOT impressed. It usually return unknown 9/10 checks
aron
+2  A: 

Google has geolocation functions in its AJAX APIs.

friol
A: 

Look at the API from hostip.info - it provides lots of information.
Example in php:> $data = file_get_contents("http://api.hostip.info/country.php?ip=12.215.42.19");
//$data = "US"
$data = file_get_contents("http://api.hostip.info/?ip=12.215.42.19");
//$data = lots of xml with country, lat, long, city, etc...

If you trust hostip.info, it seems to be a very useful API.

Isaac Waller
unf. compared to other services HostIP is waaaay off .. www.ip-adress.com however, nails it
Scott Evernden
+2  A: 

Assuming you want to do it yourself and not rely upon other providers, IP2Nation provides a MySQL database of the mappings which are updated as the regional registries change things around.

James Cape
+2  A: 

This is pretty much a duplicate of http://stackoverflow.com/questions/348614/how-do-i-determine-a-zip-code-and-city-from-an-ip-address.

Charlie Martin
True, although this one seems to be language specific (PHP) which might be interesting for those looking for a language-centric solution via Google or the tags.
lpfavreau
I should add though that it will most likely end up with the answers being the same as those services often provide API for different languages.
lpfavreau
+4  A: 

I'll make the same answer I did here as the service is available for PHP also:

I like the free GeoLite City from Maxmind which works for most applications and from which you can upgrade to a paying version if it's not precise enough. There is a PHP API included, as well as for other languages. And if you are running Lighttpd as a webserver, you can even use a module to get the information in the SERVER variable for every visitor if that's what you need.

I should add there is also a free Geolite Country (which would be faster if you don't need to pinpoint the city the IP is from) and Geolite ASN (if you want to know who owns the IP) and that finally all these are downloadable on your own server, are updated every month and are pretty quick to lookup with the provided APIs as they state "thousands of lookups per second".

lpfavreau