Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the PHP HTTP_ACCEPT_LANGUAGE server variable

I created a PHP script that checks the HTTP_ACCEPT_LANGUAGE and loads the website using the appropriate language from the 1st two characters:

          $http_lang = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2);       switch ($http_lang) {         case 'en':           $SESSION->conf['language'] = 'english';           break;         case 'es':           $SESSION->conf['language'] = 'spanish';           break;         default:           $SESSION->conf['language'] = $PREFS->conf['languages'][$SESSION->conf['language_id']];       } 

If I change the language to Spanish in Firefox the website loads in Spanish fine. However I have had several reports that people in Colombia see the website in english.

Details: "es-co" LCID = 9226 Spanish(Colombia)

Anyone have any ideas as to why this is happening? I thought this was the best way to check what language users support.

like image 447
Beanie Avatar asked May 17 '11 23:05

Beanie


People also ask

What is $_ server variable in PHP?

$_SERVER is a PHP super global variable which holds information about headers, paths, and script locations.

What is $_ server [' Script_name ']?

$_SERVER is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server. There is no guarantee that every web server will provide any of these; servers may omit some, or provide others not listed here.

What is $_ server [' Request_uri ']?

$_SERVER['REQUEST_URI'] contains the URI of the current page. So if the full path of a page is https://www.w3resource.com/html/html-tutorials.php, $_SERVER['REQUEST_URI'] would contain /html/html-tutorials. php.

How can I transfer data from one server to another in PHP?

This need not (necessarily) be a domain name, it could be the IP address of the other server. One server is 'feeding' the values in a PHP script and the other 'reads' the output of that page. If you simply need to pass data (information) between servers, you should investigate SCP.


1 Answers

A more contemporary method would be to use http_negotiate_language():

 $map = array("en" => "english", "es" => "spanish");  $conf_language= $map[ http_negotiate_language(array_keys($map)) ]; 

If you don't have the http extension installed (and not the intl one as well), there is yet another workaround in the comments (user-note #86787 (Nov 2008; by Anonymous)):

<?php  /*    determine which language out of an available set the user prefers most     $available_languages        array with language-tag-strings (must be lowercase) that are available    $http_accept_language    a HTTP_ACCEPT_LANGUAGE string (read from $_SERVER['HTTP_ACCEPT_LANGUAGE'] if left out)  */  function prefered_language ($available_languages,$http_accept_language="auto") {      // if $http_accept_language was left out, read it from the HTTP-Header      if ($http_accept_language == "auto") $http_accept_language = isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : '';       // standard  for HTTP_ACCEPT_LANGUAGE is defined under      // http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4      // pattern to find is therefore something like this:      //    1#( language-range [ ";" "q" "=" qvalue ] )      // where:      //    language-range  = ( ( 1*8ALPHA *( "-" 1*8ALPHA ) ) | "*" )      //    qvalue         = ( "0" [ "." 0*3DIGIT ] )      //            | ( "1" [ "." 0*3("0") ] )      preg_match_all("/([[:alpha:]]{1,8})(-([[:alpha:]|-]{1,8}))?" .                     "(\s*;\s*q\s*=\s*(1\.0{0,3}|0\.\d{0,3}))?\s*(,|$)/i",                     $http_accept_language, $hits, PREG_SET_ORDER);       // default language (in case of no hits) is the first in the array      $bestlang = $available_languages[0];      $bestqval = 0;       foreach ($hits as $arr) {          // read data from the array of this hit          $langprefix = strtolower ($arr[1]);          if (!empty($arr[3])) {              $langrange = strtolower ($arr[3]);              $language = $langprefix . "-" . $langrange;          }          else $language = $langprefix;          $qvalue = 1.0;          if (!empty($arr[5])) $qvalue = floatval($arr[5]);           // find q-maximal language           if (in_array($language,$available_languages) && ($qvalue > $bestqval)) {              $bestlang = $language;              $bestqval = $qvalue;          }          // if no direct hit, try the prefix only but decrease q-value by 10% (as http_negotiate_language does)          else if (in_array($langprefix,$available_languages) && (($qvalue*0.9) > $bestqval)) {              $bestlang = $langprefix;              $bestqval = $qvalue*0.9;          }      }      return $bestlang;  }  ?> 
like image 195
mario Avatar answered Sep 25 '22 08:09

mario