Home >Backend Development >PHP Tutorial >Function code to use PHP to determine browser type and language_PHP tutorial

Function code to use PHP to determine browser type and language_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:13:02831browse

We often see that some websites will display the browser type and language you are currently using. For example, it displays: The browser you are using is IE6, Traditional Chinese. Doesn’t it look cool?

In fact, such a function is not difficult to implement. It is nothing more than judging the type and language of the browser. It should be very simple if you use JS. Here we look at how to use PHP to implement such a function. Since we are making a judgment, we You can use PHP's conditional statement if..else to determine the implementation.

Determine browser type:

Determine browser language:

The specific procedure to determine the browser type is as follows:

Copy the code The code is as follows:

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
The code is as follows:

$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"];
?>



Summary: Determining the browser type mainly relies on analyzing the content of _SERVER["HTTP_USER_AGENT"], while analyzing the browser language is analyzing _SERVER["HTTP_ACCEPT_LANGUAGE"].

Principle: Because the browser will first send some content containing its own information (browser type, language) when connecting to the server. So here we mainly analyze _SERVER["HTTP_USER_AGENT"] (browser type) and _SERVER["HTTP_ACCEPT_LANGUAGE"] (browser language). All we have to do is read out these contents, and then use strpos or preg_match function to compare them. According to the design of the program, we can also design the page to be more beautiful.


http://www.bkjia.com/PHPjc/326562.html

www.bkjia.com

true

http: //www.bkjia.com/PHPjc/326562.html

We often see some websites that display the type of browser you are currently using and the language you are using, such as What is displayed is: The browser you are using is IE6, traditional Chinese characters. It doesn’t look like...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP function code to delete a folder and all files under the folder_PHP tutorialNext article:PHP function code to delete a folder and all files under the folder_PHP tutorial

Related articles

See more