How to get user's browser name and append it as a class to <body> tag
Question
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.
I want to append the user's browser name to my <body>
so I can target them via css later
Example how the PHP should render the name to my body tag.
<body class="ie5" >
or
<body class="safari" >
CSS Sample I'll use later
body.ie div#id { background: #ccc }
body.safari div#id { font-size: 15px; }
Solution
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 ." >
OTHER TIPS
$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 }";
}
You could use the variable $_SERVER['HTTP_USER_AGENT']
:
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
<?php $agent=$_SERVER['HTTP_USER_AGENT']?>
Then in HTML:
<html>
<head>
</head>
<body class="<?php echo $agent?>">
</body>
</html>
Then just setup your CSS
for each browser name/type
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow