PHP code to determine browser type, browser language and other information

WBOY
Release: 2016-07-25 09:04:23
Original
1101 people have browsed it
  1. //Determine the browser type
  2. echo $_SERVER["HTTP_USER_AGENT"];
  3. //Determine the browser language
  4. echo $_SERVER["HTTP_ACCEPT_LANGUAGE"];
  5. ?>
Copy the code

The following are two complete examples to determine the browser type and browser language respectively. 1. Determine browser type

  1. if(strpos($_SERVER["HTTP_USER_AGENT"],"MSIE 8.0"))
  2. echo "Internet Explorer 8.0";
  3. else if(strpos($_SERVER["HTTP_USER_AGENT"] ,"MSIE 7.0"))
  4. echo "Internet Explorer 7.0";
  5. else if(strpos($_SERVER["HTTP_USER_AGENT"],"MSIE 6.0"))
  6. echo "Internet Explorer 6.0";
  7. else if(strpos($ _SERVER["HTTP_USER_AGENT"],"Firefox/3"))
  8. echo "Firefox 3";
  9. else if(strpos($_SERVER["HTTP_USER_AGENT"],"Firefox/2"))
  10. echo "Firefox 2";
  11. else if(strpos($_SERVER["HTTP_USER_AGENT"],"Chrome"))
  12. echo "Google Chrome";
  13. else if(strpos($_SERVER["HTTP_USER_AGENT"],"Safari"))
  14. echo "Safari";
  15. else if(strpos($_SERVER["HTTP_USER_AGENT"],"Opera"))
  16. echo "Opera";
  17. else echo $_SERVER["HTTP_USER_AGENT"];
  18. ?>
Copy code

2. Determine browser language

  1. $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 4); //Only take the first 4 digits, so that only the highest priority language is judged. If you take the first 5 digits, en and zh may occur, which affects the judgment.
  2. if (preg_match("/zh-c/i", $lang))
  3. echo "Simplified Chinese";
  4. else if (preg_match("/zh/i", $lang))
  5. echo "Traditional Chinese";
  6. else if (preg_match("/en/i", $lang))
  7. echo "English";
  8. else if (preg_match("/fr/i", $lang))
  9. echo "French";
  10. else if (preg_match ("/de/i", $lang))
  11. echo "German";
  12. else if (preg_match("/jp/i", $lang))
  13. echo "Japanese";
  14. else if (preg_match("/ko /i", $lang))
  15. echo "Korean";
  16. else if (preg_match("/es/i", $lang))
  17. echo "Spanish";
  18. else if (preg_match("/sv/i", $lang))
  19. echo "Swedish";
  20. else echo $_SERVER["HTTP_ACCEPT_LANGUAGE"];
  21. /*@ http://bbs.it-home.org */
  22. ?>
Copy code

From the above code, you can see that PHP determines the browser type mainly with the help of _SERVER["HTTP_USER_AGENT"], while analyzing the browser language uses _SERVER["HTTP_ACCEPT_LANGUAGE"].

In principle, the browser will always send some information including (browser type, language) when connecting to the server. We can use php global changes to $_SERVER, such as _SERVER["HTTP_USER_AGENT"] (browser type) and _SERVER["HTTP_ACCEPT_LANGUAGE"] (browser language) to obtain relevant information, and then use strpos or preg_match function to compare That's it.

Okay, this is the introduction to PHP’s determination of browser type, browser language, etc. Articles you may be interested in: PHP code to determine browser type php Get the code of the visitor’s browser How to obtain client browser and operating system information with php



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!