I've been given a script to work with for detecting browsers, to apply the correct css stylesheet. It's a little above my head, and I'm trying to determine how to make edits, to add in an if statement for this:
Mozilla/5.0 (Linux; Android 4.2.2; Nexus 7 Build/JDQ39) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.123 Safari/537.22
Right now, its defaulting to the final else statement for gecko. I want to create a statement that redirects android tablet users. Dealing with these user agents is an entirely new thing for me.
I tried adding a simple statement like this:
else if(strstr(css_browser_selector(),"Android") == true && strstr(css_browser_selector(),"Chrome") == true){
?>
<link rel="stylesheet" media="screen" type="text/css" href="<?php echo ('css/test.css')?>" />
<?php echo "test";
}
But it still, jumps to the last else statement.
<?php
$iphone_test = strstr(css_browser_selector(),"iphone");
$ipad_test = strstr(css_browser_selector(),"ipad");
if($iphone_test == true || $ipad_test == true)
{
$mobile_active = true;
}
else
{
$mobile_active = false;
}
$ie9_test = strstr(css_browser_selector(),"ie9");
$ie10_test = strstr(css_browser_selector(),"ie10");
if($ie9_test == true || $ie10_test == true)
{
$ieX_active = true;
}
else
{
$ieX_active = false;
}
if(strstr(css_browser_selector(),"webkit") == true && $mobile_active == false && strstr(css_browser_selector(),"chrome") == false){
?>
<link rel="stylesheet" media="screen" type="text/css" href="<?php echo ('css/safari.css') ?>" />
<?php
}
else if(strstr(css_browser_selector(),"iphone") == true){
?>
<link rel="stylesheet" media="screen" type="text/css" href="<?php echo ('css/iphone.css')?>" />
<?php
}
else if(strstr(css_browser_selector(),"ipad") == true){
?>
<link rel="stylesheet" media="screen" type="text/css" href="<?php echo ('css/ipad.css')?>" />
<?php
}
else if(strstr(css_browser_selector(),"chrome") == true && strstr(css_browser_selector(),"win") == true){
?>
<link rel="stylesheet" media="screen" type="text/css" href="<?php echo ('css/chrome.css')?>" />
<?php
}
else if(strstr(css_browser_selector(),"ie") == true && $ieX_active == false){
?>
<link rel="stylesheet" media="screen" type="text/css" href="<?php echo ('css/ie.css')?>" />
<?php
}
else if($ieX_active == true){
?>
<link rel="stylesheet" media="screen" type="text/css" href="<?php echo ('css/ieX.css')?>" />
<?php
}
else
{
?>
<link rel="stylesheet" media="screen" type="text/css" href="<?php echo ('css/gecko.css')?>" />
<?php echo "test";
}
?>
If needed, this script is included also.
<?php
/*
PHP CSS Browser Selector v0.0.1
Bastian Allgeier (http://bastian-allgeier.de)
http://bastian-allgeier.de/css_browser_selector
License: http://creativecommons.org/licenses/by/2.5/
Credits: This is a php port from Rafael Lima's original Javascript CSS Browser Selector: http://rafael.adm.br/css_browser_selector
*/
function css_browser_selector($ua=null) {
$ua = ($ua) ? strtolower($ua) : strtolower($_SERVER['HTTP_USER_AGENT']);
$g = 'gecko';
$w = 'webkit';
$s = 'safari';
$b = array();
// browser
if(!preg_match('/opera|webtv/i', $ua) && preg_match('/msie\s(\d)/', $ua, $array)) {
$b[] = 'ie ie' . $array[1];
} else if(strstr($ua, 'firefox/2')) {
$b[] = $g . ' ff2';
} else if(strstr($ua, 'firefox/3.5')) {
$b[] = $g . ' ff3 ff3_5';
} else if(strstr($ua, 'firefox/3')) {
$b[] = $g . ' ff3';
} else if(strstr($ua, 'gecko/')) {
$b[] = $g;
} else if(preg_match('/opera(\s|\/)(\d+)/', $ua, $array)) {
$b[] = 'opera opera' . $array[2];
} else if(strstr($ua, 'konqueror')) {
$b[] = 'konqueror';
} else if(strstr($ua, 'chrome')) {
$b[] = $w . ' ' . $s . ' chrome';
} else if(strstr($ua, 'iron')) {
$b[] = $w . ' ' . $s . ' iron';
} else if(strstr($ua, 'applewebkit/')) {
$b[] = (preg_match('/version\/(\d+)/i', $ua, $array)) ? $w . ' ' . $s . ' ' . $s . $array[1] : $w . ' ' . $s;
} else if(strstr($ua, 'mozilla/')) {
$b[] = $g;
}
// platform
if(strstr($ua, 'j2me')) {
$b[] = 'mobile';
} else if(strstr($ua, 'iphone')) {
$b[] = 'iphone';
} else if(strstr($ua, 'ipad')) {
$b[] = 'ipad';
} else if(strstr($ua, 'ipod')) {
$b[] = 'ipod';
} else if(strstr($ua, 'mac')) {
$b[] = 'mac';
} else if(strstr($ua, 'darwin')) {
$b[] = 'mac';
} else if(strstr($ua, 'webtv')) {
$b[] = 'webtv';
} else if(strstr($ua, 'win')) {
$b[] = 'win';
} else if(strstr($ua, 'freebsd')) {
$b[] = 'freebsd';
} else if(strstr($ua, 'x11') || strstr($ua, 'linux')) {
$b[] = 'linux';
}
return join(' ', $b);
}
?>