- // Determine the browser type
- Echo $ _ SERVER ["HTTP_USER_AGENT"];
- // Determine the browser language
- Echo $ _ SERVER ["HTTP_ACCEPT_LANGUAGE"];
- ?>
The following are two complete examples to determine the browser type and language respectively. 1,Determine the browser type
- If (strpos ($ _ SERVER ["HTTP_USER_AGENT"], "MSIE 8.0 "))
- Echo & quot; Internet Explorer 8.0 & quot ";
- Else if (strpos ($ _ SERVER ["HTTP_USER_AGENT"], "MSIE 7.0 "))
- Echo & quot; Internet Explorer 7.0 & quot ";
- Else if (strpos ($ _ SERVER ["HTTP_USER_AGENT"], "MSIE 6.0 "))
- Echo & quot; Internet Explorer 6.0 & quot ";
- Else if (strpos ($ _ SERVER ["HTTP_USER_AGENT"], "Firefox/3 "))
- Echo "Firefox 3 ";
- Else if (strpos ($ _ SERVER ["HTTP_USER_AGENT"], "Firefox/2 "))
- Echo "Firefox 2 ";
- Else if (strpos ($ _ SERVER ["HTTP_USER_AGENT"], "Chrome "))
- Echo "Google Chrome ";
- Else if (strpos ($ _ SERVER ["HTTP_USER_AGENT"], "Safari "))
- Echo "Safari ";
- Else if (strpos ($ _ SERVER ["HTTP_USER_AGENT"], "Opera "))
- Echo "Opera ";
- Else echo $ _ SERVER ["HTTP_USER_AGENT"];
- ?>
2,Determine the browser language
- $ 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.
- If (preg_match ("/zh-c/I", $ lang ))
- Echo "simplified Chinese ";
- Else if (preg_match ("/zh/I", $ lang ))
- Echo "traditional Chinese ";
- Else if (preg_match ("/en/I", $ lang ))
- Echo "English ";
- Else if (preg_match ("/fr/I", $ lang ))
- Echo "French ";
- Else if (preg_match ("/de/I", $ lang ))
- Echo "German ";
- Else if (preg_match ("/jp/I", $ lang ))
- Echo "Japan ";
- Else if (preg_match ("/ko/I", $ lang ))
- Echo "Korean ";
- Else if (preg_match ("/es/I", $ lang ))
- Echo "Spanish ";
- Else if (preg_match ("/sv/I", $ lang ))
- Echo "Swedish ";
- Else echo $ _ SERVER ["HTTP_ACCEPT_LANGUAGE"];
- /* @ Http://bbs.it-home.org */
- ?>
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 |