如何获取用户的浏览器名称并将其作为类附加到<body>标签


How to get user's browser name and append it as a class to <body> tag

我正在为 Concret 5 做一个主题,我不确定 Concrete 5 中是否已经有类似的解决方案来解决我的问题。

我想将用户的浏览器名称附加到我的<body>以便以后可以通过 css 定位他们

示例 PHP 应该如何将名称呈现为我的身体标签。

<body class="ie5" >
or
<body class="safari" >

我稍后将使用的 CSS 示例

body.ie div#id { background: #ccc }
body.safari div#id { font-size: 15px; }

根据您之前未问过的其他要求

用于获取所有浏览器的名称

        $u_agent = $_SERVER['HTTP_USER_AGENT'];
        $ub = '';
        if(preg_match('/MSIE/i',$u_agent))
        {
            $ub = "Internet Explorer";
        }
        elseif(preg_match('/Firefox/i',$u_agent))
        {
            $ub = "Mozilla Firefox";
        }
        elseif(preg_match('/Safari/i',$u_agent))
        {
            $ub = "Apple Safari";
        }
        elseif(preg_match('/Chrome/i',$u_agent))
        {
            $ub = "Google Chrome";
        }
        elseif(preg_match('/Flock/i',$u_agent))
         {
            $ub = "Flock";
        }
        elseif(preg_match('/Opera/i',$u_agent))
        {
            $ub = "Opera";
        }
        elseif(preg_match('/Netscape/i',$u_agent))
        {
            $ub = "Netscape";
        }

现在您可以显示浏览器名称

<body class="'. $ub ." >
$u_agent = $_SERVER['HTTP_USER_AGENT']; // get the current browser name

// for safari 
if(preg_match('/Safari/i',$u_agent)) // then check if the browser is safari 
        {
           echo "body.safari div#id { font-size: 15px; }"; // then apply your class here
        }
// for IE
if(preg_match('/MSIE/i',$u_agent))
        {
            echo "body.ie div#id { background: #ccc }";
        }

>您可以使用变量$_SERVER['HTTP_USER_AGENT']

http://php.net/manual/en/reserved.variables.server.php

<?php
echo $_SERVER['HTTP_USER_AGENT'];
?>

Firefox 的示例值为:Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20040803 Firefox/0.9.3

<?php $agent=$_SERVER['HTTP_USER_AGENT']?>

然后在 HTML 中:

<html>
    <head>
    </head>
    <body class="<?php echo $agent?>">
    </body>
</html>

然后只需为每个浏览器名称/类型设置CSS