Php judgment browser and language _ PHP Tutorial

Source: Internet
Author: User
Php judges the browser and language. 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 ). 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:

Determine the browser language:

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

01

02 if (strpos ($ _ SERVER ["HTTP_USER_AGENT"], "MSIE 8.0 "))

03 echo "Internet Explorer 8.0 ";

04 else if (strpos ($ _ SERVER ["HTTP_USER_AGENT"], "MSIE 7.0 "))

05 echo "Internet Explorer 7.0 ";

06 else if (strpos ($ _ SERVER ["HTTP_USER_AGENT"], "MSIE 6.0 "))

07 echo "Internet Explorer 6.0 ";

08 else if (strpos ($ _ SERVER ["HTTP_USER_AGENT"], "Firefox/3 "))

09 echo "Firefox 3 ";

10 else if (strpos ($ _ SERVER ["HTTP_USER_AGENT"], "Firefox/2 "))

11 echo "Firefox 2 ";

12 else if (strpos ($ _ SERVER ["HTTP_USER_AGENT"], "Chrome "))

13 echo "Google Chrome ";

14 else if (strpos ($ _ SERVER ["HTTP_USER_AGENT"], "Safari "))

15 echo "Safari ";

16 else if (strpos ($ _ SERVER ["HTTP_USER_AGENT"], "Opera "))

17 echo "Opera ";

18 else echo $ _ SERVER ["HTTP_USER_AGENT"];

19?>

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

01

02 $ 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.

03 if (preg_match ("/zh-c/I", $ lang ))

04 echo "simplified Chinese ";

05 else if (preg_match ("/zh/I", $ lang ))

06 echo "traditional Chinese ";

07 else if (preg_match ("/en/I", $ lang ))

08 echo "English ";

09 else if (preg_match ("/fr/I", $ lang ))

10 echo "French ";

11 else if (preg_match ("/de/I", $ lang ))

12 echo "German ";

13 else if (preg_match ("/jp/I", $ lang ))

14 echo "Japanese ";

15 else if (preg_match ("/ko/I", $ lang ))

16 echo "Korean ";

17 else if (preg_match ("/es/I", $ lang ))

18 echo "Spanish ";

19 else if (preg_match ("/sv/I", $ lang ))

20 echo "Swedish ";

21 else echo $ _ SERVER ["HTTP_ACCEPT_LANGUAGE"];

22?>


To determine the browser type, analyze the content of _ SERVER ["HTTP_USER_AGENT"]. to analyze the browser language, analyze _ SERVER ["HTTP_ACCEPT_LANGUAGE"].


This article is from "wolf blog"

Bytes. Because when the browser connects to the server, it will first send some content containing its own information (browser type, language )....

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.