PHP determines the browser type and the browser language (with country-specific abbreviated code)

Source: Internet
Author: User

Using PHP to determine the browser type is actually very easy.

This is because when the browser connects to the server, it will first send some content containing its own information (browser type and language ).

Here we mainly analyze _ server ["http_user_agent"] (browser type) and _ server ["http_accept_language"] (Browser language ).

 

All we need to do is read the content and use the strpos or preg_match function for comparison.

Determine the browser type:

<?php echo $_SERVER["HTTP_USER_AGENT"]; ?>  

Determine the browser language:

<?php echo $_SERVER["HTTP_ACCEPT_LANGUAGE"]; ?>

The specific program for determining the browser type is as follows:

<?phpif(strpos($_SERVER["HTTP_USER_AGENT"],"MSIE 8.0"))echo "Internet Explorer 8.0";else if(strpos($_SERVER["HTTP_USER_AGENT"],"MSIE 7.0"))echo "Internet Explorer 7.0";else if(strpos($_SERVER["HTTP_USER_AGENT"],"MSIE 6.0"))echo "Internet Explorer 6.0";else if(strpos($_SERVER["HTTP_USER_AGENT"],"Firefox/3"))echo "Firefox 3";else if(strpos($_SERVER["HTTP_USER_AGENT"],"Firefox/2"))echo "Firefox 2";else if(strpos($_SERVER["HTTP_USER_AGENT"],"Chrome"))echo "Google Chrome";else if(strpos($_SERVER["HTTP_USER_AGENT"],"Safari"))echo "Safari";else if(strpos($_SERVER["HTTP_USER_AGENT"],"Opera"))echo "Opera";else echo $_SERVER["HTTP_USER_AGENT"];?>

The specific procedure for judging the browser language is as follows:

<? PHP $ lang = substr ($ _ server ['HTTP _ accept_language '], 0, 4); // only the first four digits are used to determine the preferred language. If the first five digits are obtained, en and ZH may occur, affecting the judgment. If (preg_match ("/ZH-C/I", $ Lang) echo "simplified Chinese"; else if (preg_match ("/zh/I", $ Lang )) echo "Traditional Chinese"; else if (preg_match ("/en/I", $ Lang) echo "English"; else if (preg_match ("/fr/I ", $ Lang) echo "French"; else if (preg_match ("/DE/I", $ Lang) echo "German "; else if (preg_match ("/jp/I", $ Lang) echo "Japanese"; else if (preg_match ("/KO/I", $ Lang )) echo "Korean"; else if (preg_match ("/ES/I", $ Lang) echo "Spanish"; else If (preg_match ("/SV/I", $ Lang) echo "Swedish"; else echo $ _ server ["http_accept_language"];?>

To determine the browser type, analyze the content of _ server ["http_user_agent"]. To analyze the browser language, analyze _ server ["http_accept_language"].

Or:

function getPreferredLanguage() {$langs = array();if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {// break up string into pieces (languages and q factors)preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)s*(;s*qs*=s*(1|0.[0-9]+))?/i',$_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse);if (count($lang_parse[1])) {// create a list like "en" => 0.8$langs = array_combine($lang_parse[1], $lang_parse[4]);// set default to 1 for any without q factorforeach ($langs as $lang => $val) {if ($val === '') $langs[$lang] = 1;}// sort list based on valuearsort($langs, SORT_NUMERIC);}}//extract most important (first)foreach ($langs as $lang => $val) { break; }//if complex language simplify itif (stristr($lang,"-")) {$tmp = explode("-",$lang); $lang = $tmp[0]; }return $lang;}

$ _ Server ['HTTP _ accept_language '] Get the current language
Afrikaans (AF)
Albanian (SQ)
Basque (EU)
Bulgarian (BG)
Byelorussian (be)
Catalan (CA)
Chinese (zh)
Chinese/China (zh-CN)
Chinese/Taiwan (zh-tw)
Chinese/Hong Kong (zh-HK)
Chinese/Singapore (zh-SG)
Croatian (HR)
Czech (CS)
Danish (DA)
Dutch (NL)
Dutch/Belgium (NL-be)
English (en)
English/United Kingdom (En-GB)
English/United Satates (En-US)
English/English Alian (En-Au)
English/Canada (En-Ca)
English/New Zealand (En-NZ)
English/Ireland (En-ie)
English/South Africa (En-za)
English/Jamaica (En-JM)
English/Belize (En-BZ)
English/Trinidad (En-TT)
Estonian (ET)
Faeroese (FO)
Farsi (FA)
Finnish (FI)
French (FR)
French/Belgium (fr-be)
French/France (fr-fr)
French/Switzerland (fr-Ch)
French/Canada (fr-Ca)
French/Luxembourg (fr-Lu)
Gaelic (GD)
Galician (GL)
German (de)
German/Austria (de-)
German/Germany (de-de)
German/Switzerland (de-Ch)
German/Luxembourg (de-Lu)
German/Liechtenstein (de-Li)
Greek (EL)
Hindi (HI)
Hungarian (HU)
Icelandic (is)
Indonesian (ID or in)
Irish (GA)
Italian (it)
Italian/Switzerland (IT-Ch)
Japan (JA)
Korean (KO)
Latvian (LV)
Lithuanian (LT)
Macedonian (MK)
Malaysian (MS)
Maltese (MT)
Norwegian (NO)
Polish (PL)
Portuguese (PT)
Portuguese/Brazil (Pt-Br)
Rhaeto-Romanic (RM)
Romanian (RO)
Romanian/Moldavia (Ro-Mo)
Russian (Ru)
Russian/Moldavia (Ru-Mo)
Scots Gaelic (GD)
Serbian (SR)
Slovack (SK)
Slovenian (SL)
Sorbian (SB)
Spanish (ES or es-Do)
Spanish/Argentina (ES-AR)
Spanish/Colombia (ES-Co)
Spanish/Mexico (ES-mx)
Spanish/Spain (ES-es)
Spanish/Guatemala (ES-GT)
Spanish/Costa Rica (ES-Cr)
Spanish/Panama (ES-PA)
Spanish/Venezuela (ES-ve)
Spanish/Peru (ES-PE)
Spanish/Ecuador (ES-EC)
Spanish/Chile (ES-cl)
Spanish/Uruguay (ES-uy)
Spanish/Paraguay (ES-Py)
Spanish/Bolivia (ES-bo)
Spanish/El Salvador (ES-SV)
Spanish/Honduras (ES-HN)
Spanish/Nicaragua (ES-Ni)
Spanish/Puerto Rico (ES-Pr)
Susuu (SX)
Swedish (SV)
Swedish/findland (SV-fi)
Thai (TS)
Tswana (TN)
Turkish (TR)
Ukrainian (UK)
Urdu (UR)
Vietnamese (VI)
Xshosa (XH)
Yiddish (JI)
Zulu (zu)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.