tags:

views:

3935

answers:

9

I would like to be able to detect what country a visitor is from on my website, using PHP.

Please note that I'm not trying to use this as a security measure or for anything important, just changing the spelling of some words (Americans seems to believe that the word "enrolment" has 2 Ls.... crazy yanks), and perhaps to give a default option in a "Select your country" list.

As such, using a Geolocation database is a tad over-the-top and I really don't want to muck about with installing new PHP libraries just for this, so what's the easiest/simplest way to find what country a visitor is from?

+1  A: 

You can do some IP comparaison without having a whole library to do it.

Solution #1

Use an API, this way nothing is needed from your side. This is a web API that let you know the country:

 Example: http://api.hostip.info/get_html.php?ip=12.215.42.19
 Return : Country: UNITED STATES (US)

Solution #2

But, Have you think to use the browser agent language? You might be able to know the type of english from it.

Solution #3

This website called BlockCountry let you have a list of IP by country. Of course, you do not want to block, but you can use the list of IP and compare them (get all US IP...) this might not be accurate...

Daok
do you have more information about http://api.hostip.info/. who runs it/how reliable it is in performance and quality of data?
Simon_Weaver
+9  A: 

Not guaranteed, but most browsers submit an Accept-Language HTTP header that specifies en-us if they're from the US. Some older browsers only said they are en, though. And not all machines are set up correctly to indicate which locale they prefer. But it's a good first guess.

English-UK based-users usually set their system or user locale to English-UK, which in default browser configurations should result in en-gb as the Accept Language header. (An earlier version of this said en-uk; that was a typo, sorry.) Other countries also have en locales, such as en-za (south africa), and, primarily theoretically, combinations like en-jp are also possible.

Geo-IP based guesses will less likely be correct on the preferred language/locale, however. Google thinks that content-negotiation based on IP address geolocation makes sense, which really annoys me when I'm in Japan or Korea...

JasonTrue
+1 for the Google comment - very annoying
Ken
The client’s preferred language is not an indicator for his locale.
Gumbo
Depends on what you mean by "locale". Presuming you don't mean "location", but are using it in the software internationalization sense: The Accept-Language parameter can and now usually does specify a complete locale in each preferred language specified, since the most common use of the second half of the language field specifies the country (rather than a language variant). The combination of language plus country corresponds neatly to locale-specific conventions for date, time, number and currency conventions. Without the country information, obviously it isn't complete enough to do that.
JasonTrue
A: 

For identifying your visitors country I've used GeoIP extension, very simple to use.

CMS
+6  A: 

You can check out the HTTP_ACCEPT_LANGUAGE header (from $_SERVER) that most browsers will send.

Take a look at Zend_Locale for an example, or maybe you might even want to use the lib.

Christopher Nadeau
A: 

The http://countries.nerd.dk service is what I use for IP-to-country mapping. It works really well and being based on DNS, is cached well too.

You can also download the database for local use if you don't want to rely on an external service.

Greg Hewgill
+2  A: 

Given your stated purpose, the Accept-Language header is a more suitable solution than IP-based geolocation. Indeed, it's precisely the intended purpose of Accept-Language.

Dave Sherohman
The client’s preferred language is not an indicator for his locale.
Gumbo
True, but OP's stated reason for wanting the locale was "just changing the spelling of some words (Americans seems to believe that the word "enrolment" has 2 Ls.... crazy yanks)" - he wanted the locale so he could use it as a proxy for preferred language.
Dave Sherohman
A: 

Parse $_SERVER["HTTP_ACCEPT_LANGUAGE"] to get country and browser's locale

Slipo
+1  A: 

GeoIP extension I think is good choice, but how to make this automatic set language of content of site .... ex : somebody viewing website pakaian bayi from Holland, how to make content automatic set to holland language ....? sorry for my bad english ... :) thank b 4

pakaian bayi
A: 

You could also detect the time-zone of the client - see http://unmissabletokyo.com/country-detector.html for an example

Tom