how to detect internet explorer and firefox using PHP?

前端 未结 6 688
傲寒
傲寒 2021-01-29 09:05

how to detect internet explorer and firefox using PHP?

相关标签:
6条回答
  • 2021-01-29 09:09

    So that's not what you asked for. But it's often more senseful to differentiate on features:

     if (stristr($_SERVER["HTTP_ACCEPT"], "application/xhtml+xml")) {
          // Firefox, Safari, Opera, Chrome, IE9
     }
     else {
          // IE5,IE6,IE7,IE8
     }
    

    None of the garbage versions of IE supports XHTML for example. So that's a good way to separate browsers. Note how IE9 however counts into the newer class, and might actually be treated comparable to Firefox.

    0 讨论(0)
  • 2021-01-29 09:12

    If you need something just to put some HTML code (like different stylesheet attached) use something like that (instead of server-side code).

    <!--[if IE]>
            <link rel="stylesheet" type="text/css" href="ie.css" />
    <![endif]-->
    

    Otherwise use the HTTP_ACCEPT solution because it is based on browser features not just name. Especially that a lot of IE-addons (and some spyware) change some parts of useragent.

    0 讨论(0)
  • 2021-01-29 09:14

    The easiest way is to use PHP's get_browser function, as this will parse the HTTP User-Agent header for you and extract the relevant browser, version, platform, etc. information into an array or object as required.

    Running this (in array mode for the purposes of this example) will return a data structure in the following format (using the current php_browscap.ini file from the Browser Capabilities Project as of 15th Jan 2011):

    Array
    (
        [browser_name_regex] => �^mozilla/5\.0 \(windows; u; windows nt 6\.1; .*\) applewebkit/.* \(khtml, like gecko\) chrome/8\..* safari/.*$�
        [browser_name_pattern] => Mozilla/5.0 (Windows; U; Windows NT 6.1; *) AppleWebKit/* (KHTML, like Gecko) Chrome/8.* Safari/*
        [parent] => Chrome 8.0
        [browser] => Chrome
        [platform] => Win7
        [version] => 8.0
        [majorver] => 8
        [win32] => 1
        [frames] => 1
        [iframes] => 1
        [tables] => 1
        [cookies] => 1
        [javaapplets] => 1
        [javascript] => 1
        [cssversion] => 3
        [supportscss] => 1
        [minorver] => 0
        [alpha] => 
        [beta] => 
        [win16] => 
        [win64] => 
        [backgroundsounds] => 
        [cdf] => 
        [vbscript] => 
        [activexcontrols] => 
        [isbanned] => 
        [ismobiledevice] => 
        [issyndicationreader] => 
        [crawler] => 
        [aol] => 
        [aolversion] => 0
    )
    

    N.B.: As per the PHP manual page:

    "In order for this to work, your browscap configuration setting in php.ini must point to the correct location of the browscap.ini file on your system."

    0 讨论(0)
  • 2021-01-29 09:15

    you can use $_SERVER['HTTP_USER_AGENT'] as found in the php manual.

    However, be aware that this can be changed by the user and some browsers even provide the ability to do this VERY easy (e.g. Konqueror). A lot of plugins are available to do the same.
    Never ever trust this string.

    0 讨论(0)
  • 2021-01-29 09:22
     if (isset($_SERVER['HTTP_USER_AGENT'])) {
       $useragent   = $_SERVER['HTTP_USER_AGENT'];
     }
    
     if (strlen(strstr($useragent  , 'MSIE')) > 0) {
        $browser = 'internet explorer';
     }else if (strlen(strstr($useragent  , 'Firefox')) > 0) {
        $browser = 'firefox';
     }else{
       $browser = 'others';
     }
    
     echo $browser;
    
    0 讨论(0)
  • 2021-01-29 09:23

    To detect Firefox

    $isFirefox = (strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') > -1);
    

    To detect IE

    $isIe = (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE ') > -1);
    
    0 讨论(0)
提交回复
热议问题