PHP Judging Browser Type program code _php tutorial

Source: Internet
Author: User
In PHP, if you want to determine the browser type operation method is very simple, we just take advantage of the global variable http_user_agent can get the user browser information, so we can use the regular to determine the type or browser version.

How PHP determines browser type and browser language because the browser sends some content that contains its own information (browser type, language) when it connects to the server.

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.
To determine the browser type:

The code is as follows Copy Code

The first part of the PHP code, some are not very full, the need for friends themselves in the gourd painting scoop to add their own. (The following code has a small error, please read the article and modify it Yourself)

code as follows copy code

if (Strpos ($_server["Http_user_agent"], "MSIE 9.0"))
echo "Internet Explorer 9.0";
else 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"))
echo "Firefox";
else if (Strpos ($_server["Http_user_agent"], "Chrome"))
echo "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"];
?>

Open Opera Browser, you can see its page request header information is as follows:

opera/9.80 (Windows NT 5.1; U Edition IBIS; ZH-CN) presto/2.10.229 version/11.61
But the value returned by Strpos ($_server["Http_user_agent", "Opera") is always "0"

The solution comparison is also relatively simple,

The code is as follows Copy Code

else if (Strpos ($_server["Http_user_agent"], "Opera"))

Replaced by

else if (Strpos ($_server["Http_user_agent"], "Pera"))

The following is a stronger can be judged by the browser user or seo/seo.html "target=" _blank "> Search engine

code as follows copy code


Function My_get_browser () {
if (empty ($_server[' http_user_agent ')) {
Return ' command line, the robot is coming! ';
}
if (False!==strpos ($_server[' http_user_agent '), ' MSIE 9.0 ') {
return ' Internet Explorer 9.0 ';
}
if (False!==strpos ($_server[' http_user_agent '), ' MSIE 8.0 ') {
return ' Internet Explorer 8.0 ';
}
if (False!==strpos ($_server[' http_user_agent '), ' MSIE 7.0 ') {
return ' Internet Explorer 7.0 ';
}
if (False!==strpos ($_server[' http_user_agent '), ' MSIE 6.0 ') {
return ' Internet Explorer 6.0 ';
}
if (False!==strpos ($_server[' http_user_agent '), ' Firefox ') {
return ' Firefox ';
}
if (False!==strpos ($_server[' http_user_agent '), ' Chrome ') {
return ' chrome ';
}
if (False!==strpos ($_server[' http_user_agent '), ' Safari ') {
return ' Safari ';
}
if (False!==strpos ($_server[' http_user_agent '), ' opera ') {
return ' opera ';
}
if (False!==strpos ($_server[' http_user_agent '), ' 360SE ')} {
return ' 360SE ';
}
}

http://www.bkjia.com/PHPjc/628724.html www.bkjia.com true http://www.bkjia.com/PHPjc/628724.html techarticle in PHP If you want to determine the browser type operation method is very simple, we just take advantage of the global variable http_user_agent can get the user browser information, so we can use the regular ...

  • Related Article

    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.