PHP determines the type of client browser

Source: Internet
Author: User

1, through $_server[' http_user_agent '] to write a PHP to determine the browser type method.

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 ';
}
}

The main use of $_server[' http_user_agent '], this constant is used to check the browsing page of the visitors using what operating system (including version number) browser (including version number) and user preferences. The usage is simple, you can see it by printing it in the program.

2, in addition to PHP to determine the type of browser can also be through the PHP system function Get_browser () function, this function will return some of the user's browser performance data. This function uses the user's Browscap.ini file to determine the performance of the user's browser. If successful, the function returns an object or an array containing the user's browser information, or False if it fails.

Get_browser syntax

Get_browser (User_agent,return_array)

This function has two parameters, the parameter meaning is explained as follows:

User_agent is optional. Specifies the name of the HTTP user agent. The default is the value of the $HTTP _user_agent. You can bypass this parameter by setting NULL.

Return_array is optional. If this argument is set to True, this function returns an array instead of an object.

For the browser information that PHP obtains through $_server[' http_user_agent ' and get_browser, we can use the following example to understand:

<?php
echo $_server[' Http_user_agent ']. "<br/><br/>";
$browser = Get_browser (null,true);
Print_r ($browser);
?>

The result will be output:

mozilla/4.0
(Compatible; MSIE 6.0; Windows NT 5.1; SV1;. NET CLR 1.1.4322)

Array (
[Browser_name_regex] = ^mozilla/.\.0
(compatible; Msie 6\.0.*; *windows NT 5\.1.*\.net clr.*). *$
[Browser_name_pattern] = mozilla/? 0
(Compatible; MSIE 6.0*;*windows NT 5.1*.net clr*) *
[Parent] = IE 6.0
[Platform] = WinXP
[NETCLR] = 1
[Browser] = IE
[Version] = 6.0
[Majorver] = 6
[Minorver] = 0
[CSS] = 2
[Frames] = 1
[IFRAMEs] = 1
[Tables] = 1
[Cookies] = 1
[Backgroundsounds] = 1
[VBScript] = 1
[JavaScript] = 1
[Javaapplets] = 1
[ActiveXControls] = 1
[CDF] = 1
[AOL] =
[Beta] =
[Win16] =
[Crawler] =
[Stripper] =
[WAP] =
[AK] =
[SK] =
)

But using PHP's Get_browser () function to get information about the client browser is conditional, and if you use it directly, you will usually report the following error:

Warning:get_browser () [Function.get-browser]: browscap INI directive not set in ...

By querying the PHP manual, the explanation is: in order to use this function, you must add a php.ini file to the Browscap.ini file configuration, The Browscap.ini file contains all the existing browser types and their information, so you need to download the latest file, download the Browscap.ini and place it in a location on the server, Get_browser () When used, the obtained client information is compared to this file, and if found, the corresponding type is returned.

From the top it seems to want to use Get_browser () to get browser type cost is not small ah!

3. There is also a way to determine the browser type of PHP, even with a foreign cattle written by the open source code. There is a foreign site called Mavrick, which has a project on the browser, has been updated to write the browser class, I see this class can get the iphone, BlackBerry, Win, Mac, Linux, OS, BeOS and other platforms on the browser information, Functions can be said to be very powerful. To download this file, you should choose according to the PHP version you use. The specific code for this class is to download the study yourself:

Browser-v1-9

The use of this class is also very simple, as follows:

$obj _browser = new browser; Create an instance of this class
$obj _browser->getbrowser (); Call the relevant function to get the browser name
$obj _browser->getplatform (); Call the relevant function to get the system name
$obj _browser->ismobile (); The device used to determine the visitor is the iphone, ipad or computer (PC)

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