如何获取用户的浏览器名并将其添加到标记的类中?

时间:2022-09-14 23:49:24

I'm doing a theme for Concret 5 and I'm not sure if there's already a similar solution to my problem from Concrete 5.

我正在做一个关于混凝土5的主题,我不确定是否已经有了一个类似的解决方案。

I want to append the user's browser name to my <body> so I can target them via css later

我希望将用户的浏览器名称附加到我的,以便稍后通过css对它们进行定位。

Example how the PHP should render the name to my body tag.

例如,PHP应该如何将名称呈现给我的body标签。

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

CSS Sample I'll use later

稍后我将使用CSS示例。

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

4 个解决方案

#1


3  

as per your other requirement that wasn't asked before

按照你之前没有要求的其他要求。

for getting all browser's name

获取所有浏览器的名称。

        $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";
        }

now you can show browser name

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

<body class="'. $ub ." >

#2


1  

$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 }";
        }

#3


0  

You could use the variable $_SERVER['HTTP_USER_AGENT']:

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

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

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

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

A sample value for Firefox is: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20040803 Firefox/0.9.3

Firefox的一个示例值是:Mozilla/5.0 (Windows;U;Windows NT 5.1;en - us;房车:1.7)壁虎/ 20040803 Firefox / 0.9.3

#4


0  

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

Then in HTML:

然后在HTML中:

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

Then just setup your CSS for each browser name/type

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

#1


3  

as per your other requirement that wasn't asked before

按照你之前没有要求的其他要求。

for getting all browser's name

获取所有浏览器的名称。

        $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";
        }

now you can show browser name

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

<body class="'. $ub ." >

#2


1  

$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 }";
        }

#3


0  

You could use the variable $_SERVER['HTTP_USER_AGENT']:

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

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

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

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

A sample value for Firefox is: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20040803 Firefox/0.9.3

Firefox的一个示例值是:Mozilla/5.0 (Windows;U;Windows NT 5.1;en - us;房车:1.7)壁虎/ 20040803 Firefox / 0.9.3

#4


0  

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

Then in HTML:

然后在HTML中:

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

Then just setup your CSS for each browser name/type

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