- Determine browser type
- echo $_server["Http_user_agent"];
- Determine the browser language
- echo $_server["Http_accept_language"];
- ?>
Copy CodeHere are two complete examples to determine browser type, browser language, respectively. 1. Determine browser type
- if (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"];
- ?>
Copy Code2. determine the browser language
- $lang = substr ($_server[' http_accept_language '), 0, 4); Take only the first 4 digits, so that only the most preferred language is judged. If take the first 5 bits, may appear en,zh situation, influence 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 "中文版";
- 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"];
- /*@ http://bbs.it-home.org */
- ?>
Copy CodeFrom the above code, you can see PHP judging browser type, mainly with the help of _server["Http_user_agent", and the analysis of the browser language is the use of _server["Http_accept_language"]. In principle, the browser will always send some information including (browser type, language) class when connecting with the server. We can use PHP global change $_server, such as _server["Http_user_agent" (browser type) and _server["Http_accept_language"] (browser language) to obtain relevant information, Then you can compare it with Strpos or Preg_match functions. Well, it's about PHP judging browser type, browser language, and so on. You may be interested in the article: PHP judgment Browser type code PHP get visitor Browser code PHP get client browser with operating system Information method |