-
- //Determine the browser type
- echo $_SERVER["HTTP_USER_AGENT"];
- //Determine the browser language
- echo $_SERVER["HTTP_ACCEPT_LANGUAGE"];
- ?>
Copy the code
The following are two complete examples to determine the browser type and browser language respectively.
1. Determine browser type
-
- if(strpos($_SERVER["HTTP_USER_AGENT"],"MSIE 8.0"))
- echo "Internet Explorer 8.0";
- else if(strpos($_SERVER["HTTP_USER_AGENT"] ,"MSIE 7.0"))
- echo "Internet Explorer 7.0";
- else if(strpos($_SERVER["HTTP_USER_AGENT"],"MSIE 6.0"))
- echo "Internet Explorer 6.0";
- 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"];
- ?>
Copy code
2. Determine browser language
-
- $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 4); //Only take the first 4 digits, so that only the highest priority language is judged. If you take the first 5 digits, en and zh may occur, which affects 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 "Japanese";
- 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 */
- ?>
Copy code
From the above code, you can see that PHP determines the browser type mainly with the help of _SERVER["HTTP_USER_AGENT"], while analyzing the browser language uses _SERVER["HTTP_ACCEPT_LANGUAGE"].
In principle, the browser will always send some information including (browser type, language) when connecting to the server.
We can use php global changes to $_SERVER, such as _SERVER["HTTP_USER_AGENT"] (browser type) and _SERVER["HTTP_ACCEPT_LANGUAGE"] (browser language) to obtain relevant information, and then use strpos or preg_match function to compare That's it.
Okay, this is the introduction to PHP’s determination of browser type, browser language, etc.
Articles you may be interested in:
PHP code to determine browser type
php Get the code of the visitor’s browser
How to obtain client browser and operating system information with php
|