Search code examples
phpjavascripthtmlregions

Display flag according to Browser region


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.


Solution

  • 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.