Php code used to determine the browser type, browser language, and other information

Source: Internet
Author: User
Php code used to determine the browser type, browser language, and other information

  1. // Determine the browser type
  2. Echo $ _ SERVER ["HTTP_USER_AGENT"];
  3. // Determine the browser language
  4. Echo $ _ SERVER ["HTTP_ACCEPT_LANGUAGE"];
  5. ?>

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

  1. If (strpos ($ _ SERVER ["HTTP_USER_AGENT"], "MSIE 8.0 "))
  2. Echo & quot; Internet Explorer 8.0 & quot ";
  3. Else if (strpos ($ _ SERVER ["HTTP_USER_AGENT"], "MSIE 7.0 "))
  4. Echo & quot; Internet Explorer 7.0 & quot ";
  5. Else if (strpos ($ _ SERVER ["HTTP_USER_AGENT"], "MSIE 6.0 "))
  6. Echo & quot; Internet Explorer 6.0 & quot ";
  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. ?>

2,Determine the browser language

  1. $ 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.
  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 "Japan ";
  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. ?>

From the above code, we can see that php judges the browser type, mainly by using _ SERVER ["HTTP_USER_AGENT"], while the browser language is analyzed using _ SERVER ["HTTP_ACCEPT_LANGUAGE"].

In principle, when a browser connects to a server, it always sends information including the browser type and language. We can use php to globally Change $ _ SERVER, such as _ SERVER ["HTTP_USER_AGENT"] (browser type) and _ SERVER ["HTTP_ACCEPT_LANGUAGE"] (browser language) to obtain relevant information, then we can use the strpos or preg_match function for comparison.

Now, let's take a look at php's browser type judgment and browser language. Articles you may be interested in: PHP judgment of browser-type code php acquisition of visitor's browser code php method of obtaining client browser and operating system information

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.