views:

105

answers:

3

Ideally I'm trying to put together a PHP script that I can query from any web browser and it returns the Country of the IP address that accessed the PHP script.

Is this possible or is there a better solution?

A: 

Look at the built-in GeoIP functions. http://php.net/manual/en/book.geoip.php

John Franklin
They are not *really* built-in. http://www.php.net/manual/en/geoip.requirements.php
Pekka
I stand corrected.
John Franklin
+1  A: 

I use hostip.info API. Remote requests are slow so be carefull :)

Anpher
This is perfect! Saves me having to make sure my php server has the right modules and returns nice and simple country codes! Nice
Tristan
+3  A: 

There are free, easy APIs you can use, like those:

Which one looks the most trustworthy is up to you :)

Otherwise, there are scripts which are based on local databases on your server. The database data needs to be updated regularly, though. Check out this one:

HTH!

Edit: And of course, depending on your project you might want to look at HTML5 Location features. You can't use them yet on the Internet Explorer (IE9 will support it, long way to go yet), but in case your audience is mainly on mobile devices or using Safari/Firefox it's definitely worth to look at it!

Once you have the coordinates, you can reverse geocode them to a country code. Again there are APIs like this one:

sprain