Using PHP to determine the browser's type and language function code _php tutorial

Source: Internet
Author: User
We often see some websites that show the type of browser you are currently using and the language you are using, such as: The browser you are using is IE6, traditional. It seems to be not very flashy.

In fact, such a function is not difficult to achieve, nothing is to judge the browser type and language, if you use JS to do it should be very simple, here we see how to implement this function with PHP, since it is in the judgment, you can use the PHP conditional statement if. Else to determine the implementation.

To determine the browser type:

To determine the browser language:

The specific program that determines the browser type is as follows:
copy code The code is as follows:
!--? php if (Strpos ($_server["Http_user_agent"), "MSIE 8.0")
echo "Internet Explorer 8.0"; Br>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"];
?


The specific procedures for judging the browser language are as follows:

Copy the Code code as follows:
$lang = substr ($_server[' http_accept_language '), 0, 4); Take only the first 4 digits, so that only the most preferred language is judged. If take the first 5 bits, may appear en,zh situation, influence 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 "中文版";
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: To determine the browser type is mainly by analyzing the content of _server["Http_user_agent", while the analysis of the browser language is the analysis _server["Http_accept_language".

Principle: Because the browser will send some content (browser type, language) with its own information when connecting with the server. So our main analysis here is _server["http_user_agent" (browser type) and _server["Http_accept_language"] (browser language). All we have to do is read it out and then use the Strpos or Preg_match function to compare it, according to the design of the program we can also design the page more beautiful out.

http://www.bkjia.com/PHPjc/326562.html www.bkjia.com true http://www.bkjia.com/PHPjc/326562.html techarticle we often see some websites that show the type of browser you are currently using and the language you are using, such as: The browser you are using is IE6, traditional. Looks like it's not ...

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