Code for PHP to determine browser type, browser language, and other information

Source: Internet
Author: User
    1. Determine browser type
    2. echo $_server["Http_user_agent"];
    3. Determine the browser language
    4. echo $_server["Http_accept_language"];
    5. ?>
Copy Code

Here are two complete examples to determine browser type, 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 the browser language

    1. $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.
    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 "中文版";
    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 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

  • Related Article

    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.