用php查找浏览器

时间:2011-02-02 16:46:39

标签: php css browser find

哪种方法可以检测用户使用php查看页面的浏览器?

我尝试使用get_browser,它提供了类似的内容:

Array (
  [browser_name_regex] => §^.*$§
  [browser_name_pattern] => * 
  [browser] => Default Browser 
  [version] => 0 
  [majorver] => 0 
  [minorver] => 0 
  [platform] => unknown 
  [alpha] => 
  [beta] => 
  [win16] => 
  [win32] => 
  [win64] => 
  [frames] => 1 
  [iframes] => 
  [tables] => 1 
  [cookies] => 
  [backgroundsounds] => 
  [cdf] => 
  [vbscript] => 
  [javaapplets] => 
  [javascript] => 
  [activexcontrols] => 
  [isbanned] => 
  [ismobiledevice] => 
  [issyndicationreader] => 
  [crawler] => 
  [cssversion] => 0 
  [supportscss] => 
  [aol] => 
  [aolversion] => 0
)

这就是我为IE7,Chrome和Mozilla firefox所获得的。

我尝试了$_SERVER['HTTP_USER_AGENT'],它给了我Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.237 Safari/534.10

我可以在这里获得一些信息。

那么请告诉我是否有更好的方法来做到这一点,这是一个很好的解决方案,用不同的浏览器显示不同CSS的HTML内容?

5 个答案:

答案 0 :(得分:1)

引用this博客:

  

浏览器检测是一项虚幻的任务,最终注定要失败。浏览器总是在变化,与这些变化保持同步是一项持续的工作。某些浏览器允许用户改变浏览器识别自身的方式,或根本不识别自身。有些防火墙阻止了浏览器识别的发送,因此没有任何浏览器检测方案是完全成功的。

注意:另外,请阅读有关浏览器检测错误操作的更多信息here

然而,这就是我曾经使用的a script

<?php

/**
 * Class to detect which browser is currently accessing the page/site
 * @author Paul Scott
 * This class is very loosely based on scripts by Gary White
 * @copyright Paul Scott
 * @package browser
 */

class browser 
{
    /**
     * @var string $name
     */
    var $name = NULL;

    /**
     * @var string $version
     */
    var $version = NULL;

    /**
     * @var $useragent
     */
    var $useragent = NULL;

    /**
     * @var string $platform
     */
    var $platform;

    /**
     * @var string aol
     */
    var $aol = FALSE;

    /**
     * @var string browser
     */
    var $browsertype;

    /**
     * Class constructor
     * @param void
     * @return void
     */
    function browser()
    {
        $agent = $_SERVER['HTTP_USER_AGENT'];
        //set the useragent property
        $this->useragent = $agent;
    }

    /**
     * Method to get the browser details from the USER_AGENT string in 
     * the PHP superglobals
     * @param void
     * @return string property platform 
     */
    function getBrowserOS()
    {
        $win = eregi("win", $this->useragent);
        $linux = eregi("linux", $this->useragent);
        $mac = eregi("mac", $this->useragent);
        $os2 = eregi("OS/2", $this->useragent);
        $beos = eregi("BeOS", $this->useragent);

        //now do the check as to which matches and return it
        if($win)
        {
            $this->platform = "Windows";
        }
        elseif ($linux)
        {
            $this->platform = "Linux"; 
        }
        elseif ($mac)
        {
            $this->platform = "Macintosh"; 
        }
        elseif ($os2)
        {
            $this->platform = "OS/2"; 
        }
        elseif ($beos)
        {
            $this->platform = "BeOS"; 
        }
        return $this->platform;
    }

    /**
     * Method to test for Opera
     * @param void
     * @return property $broswer
     * @return property version
     * @return bool false on failure
     */
    function isOpera()
    {
        // test for Opera       
        if (eregi("opera",$this->useragent))
        {
            $val = stristr($this->useragent, "opera");
            if (eregi("/", $val)){
                $val = explode("/",$val);
                $this->browsertype = $val[0];
                $val = explode(" ",$val[1]);
                $this->version = $val[0];
            }else{
                $val = explode(" ",stristr($val,"opera"));
                $this->browsertype = $val[0];
                $this->version = $val[1];
            }
            return TRUE;
        }
        else {
            return FALSE;
        }
    }

    /**
     * Method to check for FireFox
     * @param void
     * @return bool false on failure
     */ 
    function isFirefox()
    {
        if(eregi("Firefox", $this->useragent))
        {
            $this->browsertype = "Firefox"; 
            $val = stristr($this->useragent, "Firefox");
            $val = explode("/",$val);
            $this->version = $val[1];
            return true;
        }
        else {
            return FALSE;
        }
    }

    /**
     * Method to check for Konquerer
     * @param void
     * @return prop $browser
     * @return prop $version
     * @return bool true on success
     */
    function isKonqueror()
    {
        if(eregi("Konqueror",$this->useragent))
        {
            $val = explode(" ",stristr($this->useragent,"Konqueror"));
            $val = explode("/",$val[0]);
            $this->browsertype = $val[0];
            $this->version = str_replace(")","",$val[1]);
            return TRUE;
        }
        else {
            return FALSE;
        }

    }//end func

    /**
     * Method to check for Internet Explorer v1
     * @param void
     * @return bool true on success
     * @return prop $browsertype
     * @return prop $version
     */
    function isIEv1()
    {
        if(eregi("microsoft internet explorer", $this->useragent))
        {
            $this->browsertype = "MSIE"; 
            $this->version = "1.0";
            $var = stristr($this->useragent, "/");
            if (ereg("308|425|426|474|0b1", $var))
            {
                $this->version = "1.5";
            }
            return TRUE;
        }
        else {
            return FALSE;
        }
    }//end function

    /**
     * Method to check for Internet Explorer later than v1
     * @param void
     * @return bool true on success
     * @return prop $browsertype
     * @return prop $version
     */
    function isMSIE()
    {
        if(eregi("msie", $this->useragent) && !eregi("opera",$this->useragent))
        {
            $this->browsertype = "MSIE"; 
            $val = explode(" ",stristr($this->useragent,"msie"));
            $this->browsertype = $val[0];
            $this->version = $val[1];

            return TRUE;
        }
        else {
            return FALSE;
        }
    }//end function

    /**
     * Method to check for Galeon
     * @param void
     * @return bool true on success
     */
    function isGaleon()
    {
        if(eregi("galeon",$this->useragent))
        {
            $val = explode(" ",stristr($this->useragent,"galeon"));
            $val = explode("/",$val[0]);
            $this->browsertype = $val[0];
            $this->version = $val[1];
            return TRUE;
        }
        else {
            return FALSE;
        }
    }//end func

    /**
     * Now we do the tests for browsers I can't test...
     * If someone finds a bug, please report it ASAP to me please!
     */

    /**
     * Method to check for WebTV browser
     * @param void
     * @return bool true on success
     * @return prop $browsertype
     * @return prop $version
     */
    function isWebTV()
    {
        if(eregi("webtv",$this->useragent))
        {
            $val = explode("/",stristr($this->useragent,"webtv"));
            $this->browsertype = $val[0];
            $this->version = $val[1];
            return TRUE;
        }
        else {
            return FALSE;
        }
    }

    /**
     * Method to check for BeOS's NetPositive
     * @param void
     * @return bool true on success
     * @return prop $browsertype
     * @return prop $version
     */
    function isNetPositive()
    {
        if(eregi("NetPositive", $this->useragent))
        {
            $val = explode("/",stristr($this->useragent,"NetPositive"));
            $this->platform = "BeOS"; 
            $this->browsertype = $val[0];
            $this->version = $val[1];
            return TRUE;
        }
        else {
            return FALSE;
        }
    }

    /**
     * Method to check for MSPIE (Pocket IE)
     * @param void
     * @return bool true on success
     */
    function isMSPIE()
    {
        if(eregi("mspie",$this->useragent) || eregi("pocket", $this->useragent))
        {
            $val = explode(" ",stristr($this->useragent,"mspie"));
            $this->browsertype = "MSPIE"; 
            $this->platform = "WindowsCE"; 
            if (eregi("mspie", $this->useragent))
                $this->version = $val[1];
            else {
                $val = explode("/",$this->useragent);
                $this->version = $val[1];
            }
            return TRUE;
        }
        else {
            return FALSE;
        }
    }

    /**
     * Method to test for iCab
     * @param void
     * @return bool true on success
     */
    function isIcab()
    {
        if(eregi("icab",$this->useragent))
        {
            $val = explode(" ",stristr($this->useragent,"icab"));
            $this->browsertype = $val[0];
            $this->version = $val[1];
            return TRUE;
        }
        else {
            return FALSE;
        }
    }

    /**
     * Method to test for the OmniWeb Browser
     * @param void
     * @return bool True on success
     */
    function isOmniWeb()
    {
        if(eregi("omniweb",$this->useragent))
        {
            $val = explode("/",stristr($this->useragent,"omniweb"));
            $this->browsertype = $val[0];
            $this->version = $val[1];
            return TRUE;
        }
        else {
            return FALSE;
        }
    }

    /**
     * Method to check for Phoenix Browser
     * @param void
     * @return bool true on success
     */
    function isPhoenix()
    {
        if(eregi("Phoenix", $this->useragent))
        {
            $this->browsertype = "Phoenix"; 
            $val = explode("/", stristr($this->useragent,"Phoenix/"));
            $this->version = $val[1];
            return TRUE;
        }
        else {
            return FALSE;
        }
    }

    /**
     * Method to check for Firebird
     * @param void
         * @return bool true on success
     */
    function isFirebird()
    {
        if(eregi("firebird", $this->useragent))
        {
            $this->browsertype = "Firebird"; 
            $val = stristr($this->useragent, "Firebird");
            $val = explode("/",$val);
            $this->version = $val[1];
            return TRUE;
        }
        else {
            return FALSE;
        }
    }

    /**
     * Method to check for Mozilla alpha/beta
     * @param void
     * @return bool true on success
     */
    function isMozAlphaBeta()
    {
        if(eregi("mozilla",$this->useragent) && 
           eregi("rv:[0-9].[0-9][a-b]",$this->useragent) && 
           !eregi("netscape",$this->useragent))

        {
            $this->browsertype = "Mozilla"; 
            $val = explode(" ",stristr($this->useragent,"rv:"));
            eregi("rv:[0-9].[0-9][a-b]",$this->useragent,$val);
            $this->version = str_replace("rv:","",$val[0]);
            return TRUE;
        }
        else {
            return FALSE;
        }
    }//end function

    /**
     * Method to check for Mozilla Stable versions
     * @param void
     * @return bool true on success
     */
    function isMozStable()
    {
        if(eregi("mozilla",$this->useragent) &&
           eregi("rv:[0-9]\.[0-9]",$this->useragent) && 
           !eregi("netscape",$this->useragent))
        {
            $this->browsertype = "Mozilla"; 
            $val = explode(" ",stristr($this->useragent,"rv:"));
            eregi("rv:[0-9]\.[0-9]\.[0-9]",$this->useragent,$val);
            $this->version = str_replace("rv:","",$val[0]);
            return TRUE;
        }
        else {
            return FALSE;
        }
    }

    /**
     * Method to check for Lynx and Amaya
     * @param void
     * @return bool true on success
     */
    function isLynx()
    {
        if(eregi("libwww", $this->useragent))
        {
            if (eregi("amaya", $this->useragent))
            {
                $val = explode("/",stristr($this->useragent,"amaya"));
                $this->browsertype = "Amaya"; 
                $val = explode(" ", $val[1]);
                $this->version = $val[0];
            } else {
                $val = explode("/",$this->useragent);
                $this->browsertype = "Lynx"; 
                $this->version = $val[1];
            }
            return TRUE;
        }
        else {
            return FALSE;
        }
    }

    /**
     * method to check for safari browser
     * @param void
     * @return bool true on success
     */
    function isSafari()
        {
        if(eregi("safari", $this->useragent))
        {
            $this->browsertype = "Safari"; 
            $this->version = "";
            return TRUE;
        }
        else {
            return FALSE;
        }
    }

    /**
     * Various tests for Netscrape
     * @param void
     * @return bool true on success
     */
    function isNetscape()
    {
        if(eregi("netscape",$this->useragent))
        {
            $val = explode(" ",stristr($this->useragent,"netscape"));
            $val = explode("/",$val[0]);
            $this->browsertype = $val[0];
            $this->version = $val[1];
            return TRUE;
        }
        elseif(eregi("mozilla",$this->useragent) && 
                !eregi("rv:[0-9]\.[0-9]\.[0-9]",$this->useragent))
        {
            $val = explode(" ",stristr($this->useragent,"mozilla"));
            $val = explode("/",$val[0]);
            $this->browsertype = "Netscape"; 
            $this->version = $val[1];
            return TRUE;
        }
        else {
            return FALSE;
        }
    }//end func

    /**
     * Method to check for AOL connections
     * @param void
     * @return bool true on Success
     */
    function isAOL()
    {
        if (eregi("AOL", $this->useragent)){
            $var = stristr($this->useragent, "AOL");
            $var = explode(" ", $var);
            $this->aol = ereg_replace("[^0-9,.,a-z,A-Z]", "", $var[1]);
            return TRUE;
        }
        else { 
            return FALSE;
        }
    }

    /**
     * Method to tie them all up and output something useful
     * @param void
     * @return array
     */
    function whatBrowser()
    {
        $this->getBrowserOS();
        $this->isOpera();
        $this->isFirefox();
        $this->isKonqueror();
        $this->isIEv1();
        $this->isMSIE();
        $this->isGaleon();
        $this->isNetPositive();
        $this->isMSPIE();
        $this->isIcab();
        $this->isOmniWeb();
        $this->isPhoenix();
        $this->isFirebird();
        $this->isLynx();
        $this->isSafari();
        //$this->isMozAlphaBeta();
        //$this->isMozStable();
        //$this->isNetscape();
        $this->isAOL();
        return array('browsertype' => $this->browsertype, 
                     'version' => $this->version, 
                     'platform' => $this->platform, 
                     'AOL' => $this->aol); 
    }
}//end class
?>

如上所述,conditional comments可能仅适用于IE,但您可以解决约90%的问题。

另一方面,您可能会发现Zend_Locale有趣的返回浏览器语言等。

在任何情况下,您最好定位您希望最支持哪种浏览器,专注于该浏览器并最终尝试与其他浏览器兼容。 (但是,“定位标准而不是特定的浏览器”总是一个好主意。)这是一个我们都必须完成的任务(除非使用Echo2,GWT等框架)

答案 1 :(得分:1)

除非你有一个非常复杂的设计,你应该尝试为每个浏览器使用相同的css并跨浏览器优化它,如果你有一个简单明了的html结构,那么效果最好。

这会让你保持理智,因为你不需要维护很多不同的css文件,随着网站的老化,这会变得更加有效。

IE6 可能是一个非常特殊的情况,你可以使用提到的条件语句centr0

答案 2 :(得分:1)

您没有从get_browser中获取有用数据的原因是因为get_browser并非神奇。

它要求您在服务器上有一个名为browsecap.ini的文件,该文件定义了它应该如何计算浏览器的内容以及它的功能。

get_browser - http://uk3.php.net/get_browser的PHP手册页上有一条关于browsecap.ini的说明。本说明还解释了browsecap.ini不是PHP的标准配置,但提供了一个指向最新版本的链接。

每当发布任何浏览器的新版本时,网站所有者(即您)都需要保持browsecap.ini文件的最新状态,否则您将很快收到不良结果。

但是,即使使用最新的browsecap.ini文件,仍然无法依靠get_browser为您提供准确的答案,因为它着眼于确定浏览器可以被浏览器欺骗 - 例如,Firefox有一个选项可以让它假装是各种版本的IE,而大多数其他浏览器都有类似的功能。这将完全愚弄get_browser。没有办法解决这个问题。

更好的方法是在客户端使用Javascript来确定浏览器支持哪些功能。请参阅我的answer to this question以获取有关此问题的进一步建议。

我希望能回答你的问题。

答案 3 :(得分:0)

条件语句在不使用PHP的情况下工作。

http://www.quirksmode.org/css/condcom.html

答案 4 :(得分:0)

我在这个标题上发现了一个不错的博客 http://quicklesson.info/article/how-to-find-browser-name-using-php