-1

On the top of my website I want a flag to show which represents the users region, i.e. browser region.

does any one have any idea on how to achieve this?

Thanks for any ideas/suggestions

edit: after some confusion, I am talking about location not language.

RSM
  • 14,540
  • 34
  • 97
  • 144
  • 2
    What is a browser region? You mean geographically? – Šime Vidas Mar 07 '11 at 15:32
  • 1
    Do you mean the visitor's country or the browser language? – Pekka Mar 07 '11 at 15:32
  • yes. so if someone visited from china, it would display chinese flag, uk, uk flag etc – RSM Mar 07 '11 at 15:32
  • 2
    Possible duplicate of [IP to country?](http://stackoverflow.com/questions/1033/ip-to-country) – Pekka Mar 07 '11 at 15:34
  • not a duplicate, I dont want IP address, just to detect their BR (browser region) and write a script which adapts the flag shows to that of the BR – RSM Mar 07 '11 at 15:35
  • also, the question, you suggested may be a duplicate in no way shape or form resolves my issue. – RSM Mar 07 '11 at 15:38
  • 1
    There's no such thing as a "browser region". There's only browser language which isn't a good indicator of their region/country. See my answer for a solution to finding their region/country. – swalk Mar 07 '11 at 15:40
  • IE can do navigator.userLanguage – mplungjan Mar 07 '11 at 15:48
  • BTW, if a user changes the region of his computer and controls the browser via that computer, the region his browser is changes, therefore we get browser region. – RSM Mar 07 '11 at 16:03
  • @Ryan but that definition of "region" is the opposite of what you said you want. Do you want the language/region set in the browser, or the visitor's actual geographical location? – Pekka Mar 07 '11 at 16:07
  • inherently the region of a computer is geographical, but the ability to change it gives me great scope for testing. So for that purpose i would want the region set. But, I do see your point, and its tricky! It might be more 'proper' to have the actual geopraphic location? what you think? – RSM Mar 07 '11 at 16:19
  • Geolocation based on IP is the most accurate way to determine a user's geographical location. You're right that it makes it harder to test, but unfortunately there's no way to get a computer's region through their browser. My browser's language is set to en-US but I'm in Canada. – swalk Mar 07 '11 at 16:23

2 Answers2

4

Use the MaxMind GeoLite Country API to determine the user's country. They also offer a PHP module which should make it easier for you.

Once you have the user's country, you'll need to map it to a flag. Here's a free flag icon set: http://www.famfamfam.com/lab/icons/flags/

Integration details:

$gi = geoip_open('GeoIP.dat', GEOIP_MEMORY_CACHE);
$country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
geoip_close($gi);

echo '<img src="' . $country . '.png">';

You'll need to read up on the PHP module but that's the quick code on how to output an image tag with the country code.

swalk
  • 746
  • 4
  • 11
  • Thats what the question is, how to map it to a flag? – RSM Mar 07 '11 at 15:39
  • MaxMind API returns a country code and that flag icon set uses the same country code for their filenames. I edited my answer with some more details. Hopefully that helps. – swalk Mar 07 '11 at 15:41
  • @swalk Does this work: http://jsfiddle.net/simevidas/eKkAT/? My alert box says HR which is correct (I'm in Croatia). Does it work for you too? – Šime Vidas Mar 07 '11 at 15:44
  • @swalk Neat `:)` I didn't know that it's that simple. Strange though why Google doesn't offer such a service. – Šime Vidas Mar 07 '11 at 15:49
0

infodb provide a free geolocation service - or the data required to roll your own.

symcbean
  • 47,736
  • 6
  • 59
  • 94