views:

72

answers:

3

How can you apply a US style for people in US and a UK style for people in UK?

For example, I want to change a flag according to the user's location in my site.

+3  A: 

It's called geolocation: http://stackoverflow.com/questions/37015/how-can-i-find-a-users-location-based-on-their-ip-address-free-and-not-free-s


All questions tagged Geolocation

Smashery
That link appears to be broken?
Mitch Wheat
It's an illusion! It may appear that way, but in fact... hmmm... works for me. Just made an edit - still broken?
Smashery
second link goes to a blank page for me. weird
Mitch Wheat
@Mitch Wheat: That's because the last time you browsed questions you selected Bounty. Look at the tabs on the top right and chose any
phihag
ah! I was on Featured! Thx.
Mitch Wheat
Fixed the second link so it explicitly goes to ordering by votes.
Smashery
+1  A: 

If you really want the user's location, the keyword is geolocation.

For your purpose, however, you can just check $_SERVER["ACCEPT_LANGUAGE"]. The syntax is a little bit complex. Fortunately, virtually every client does what should have been done right away and orders the preferred locations. So, calculate stripos($_SERVER["ACCEPT_LANGUAGE"], $lang) for all locales $lang (like "en-us") you do support and take the lowest, discarding locales that yield false.

phihag
+3  A: 

First, you need to find your visitor's IP address. It will be in the $_SERVER['REMOTE_ADDR'] global variable. You can then look this IP up against a database of IP to country mappings to get the country code for the IP.

This method is not 100% accurate, but it is "good enough" for most uses, and more importantly it is fast. You will probably want to cache the country code in a cookie to avoid repeated lookups, if you expect high traffic.

One such mapping can be found here: http://software77.net/cgi-bin/ip-country/geo-ip.pl

A PHP class that takes an IP and returns a country code using a bundled mapping file: http://www.phpclasses.org/browse/package/2363.html

rjh