Use php to determine the browser type and language function code

Source: Internet
Author: User

We often see some websites that display the browser type and language you are currently using. For example, the browser you are using is IE6 or traditional Chinese. Does it look so dazzling.

In fact, this function is not difficult to implement. It is nothing more than determining the browser type and language. if Javascript is used, it should be very simple. Here we will look at how to implement this function using PHP, since we are making a judgment, we can use the PHP conditional statement if .. else To determine the implementation.

Determine the browser type:

<? Php echo $ _ SERVER ["HTTP_USER_AGENT"];?>

Determine the browser language:

<? Php echo $ _ SERVER ["HTTP_ACCEPT_LANGUAGE"];?>

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

Copy codeThe Code is as follows: <? Php
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"];
?>

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

Copy codeThe Code is as follows: <? Php
$ 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"];
?>

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

Principle: When the browser connects to the server, it will first send some content containing its own information (browser type and language ). So 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 then compare it with the strpos or preg_match function. According to the program design, we can also design the page more beautifully.

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.