Auto change the country list dropdown based on web browser languages

Submitted by: 
Language: 
Visitors have accessed this post 3771 times.

The following codes populate the country list into dropdown box based on browser's languages. The country information is getting from geolocation.com, free license with attribution: The geolocation data is provided by http://www.geolocation.com

  1. <html>
  2. <head>
  3. </head>
  4. <body>
  5. <?php
  6.  
  7. //Get the languages
  8. if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
  9. echo '<p>Browser Languages: ' . $_SERVER['HTTP_ACCEPT_LANGUAGE'] . '</p>';
  10. echo '<p style="margin:0px; padding:0px;">Country List <span style="font-size:9pt; display:block;">The geolocation data is provided by <a href="<a href="http://www.geolocation.com">http://www.geolocation.com</a></span></p>';
  11. " rel="nofollow">http://www.geolocation.com">http://www.geolocation.com</a></span></p>';
  12. </a>
  13. ////////////////////////////
  14. //Get the browser languages
  15. if(preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_matches)){
  16. $langs = array_combine($lang_matches[1], $lang_matches[4]);
  17. foreach ($langs as $lang => $val)
  18. if ($val === '') $langs[$lang] = 1;
  19.  
  20. arsort($langs, SORT_NUMERIC);
  21. }
  22.  
  23. //Default to english
  24. $language = 'en-us';
  25. foreach ($langs as $lang => $val){
  26. $language = $lang;
  27. break;
  28. }
  29.  
  30. //////////////////////
  31. //Load the Country File
  32. //Notes:
  33. //Download the CSV file from <a href="http://www.geolocation.com
  34. " rel="nofollow">http://www.geolocation.com
  35. </a> //Rename the CSV to country-en-us.csv (for english version),
  36. //country-zh-cn.csv (for chinese simplified version),
  37. //country-zh-tw.csv (for chinese traditional version)
  38. //and save in the same folder as the php code resided.
  39. echo '<select>';
  40. $countries = file_get_contents("country-$language.csv");
  41. if (preg_match_all('/"(.*?)","(.*?)","(.*?)","(.*?)"/', $countries, $matches)){
  42. for($nIdx=1; $nIdx < count($matches[1]); $nIdx++){
  43. $alpha2_code = $matches[1][$nIdx];
  44. $country_name = $matches[4][$nIdx];
  45. //echo $alpha2_code . ',' . $country_name . '<br/>';
  46. echo '<option value=' . $alpha2_code . '">' . $country_name . '</option>';
  47. }
  48. }
  49. echo '</select>';
  50.  
  51. }
  52. else
  53. echo 'Unable to locate browser languages';
  54. ?>
  55. </body>
  56. </html>

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.