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 )....