Browser compatibility issue using php
Posted: Sun Sep 27, 2009 7:06 pm
I know next to nothing about php, so help would be greatly appreciated!
I have a slight issue where a popout menu pops out way left of where it's supposed to be when I view the site using firefox. I want to use PHP to remedy this by checking the user's browser and then sending the right CSS properties accordingly. The code I'm using was mainly stolen from this site. However, when I tried to add a small bit below, it doesn't work. As in, the conditional statement that I added is always false whether I use Firefox or not.
I'm not sure what the heck I put in, but I'm pretty sure it's dead wrong.
Please help me fix it?
Thanks!
I have a slight issue where a popout menu pops out way left of where it's supposed to be when I view the site using firefox. I want to use PHP to remedy this by checking the user's browser and then sending the right CSS properties accordingly. The code I'm using was mainly stolen from this site. However, when I tried to add a small bit below, it doesn't work. As in, the conditional statement that I added is always false whether I use Firefox or not.
Code: Select all
//here is the stuff I stole
<?php
function getBrowserInfo() {
$SUPERCLASS_NAMES = "gecko,mozilla,mosaic,webkit";[/color]
$SUPERCLASSES_REGX = "(?:".str_replace(",", ")|(?:", $SUPERCLASS_NAMES).")";
$SUBCLASS_NAMES = "opera,msie,firefox,chrome,safari";
$SUBCLASSES_REGX = "(?:".str_replace(",", ")|(?:", $SUBCLASS_NAMES).")";
$browser = "unsupported";
$majorVersion = "0";
$minorVersion = "0";
$fullVersion = "0.0";
$os = 'unsupported';
$userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
$found = preg_match("/(?P<browser>".$SUBCLASSES_REGX.")(?:\D*)
(?P<majorVersion>\d*)(?P<minorVersion>(?:\.\d*)*)/i",
$userAgent, $matches);
if (!$found) {
$found = preg_match("/(?P<browser>".$SUPERCLASSES_REGX.")(?:\D*)
(?P<majorVersion>\d*)(?P<minorVersion>(?:\.\d*)*)/i",
$userAgent, $matches);
}
if ($found) {
$browser = $matches["browser"];
$majorVersion = $matches["majorVersion"];
$minorVersion = $matches["minorVersion"];
$fullVersion = $matches["majorVersion"].$matches["minorVersion"];
if ($browser != "safari") {
if (preg_match("/version\/(?P<majorVersion>\d*)
(?P<minorVersion>(?:\.\d*)*)/i",
$userAgent, $matches)){
$majorVersion = $matches["majorVersion"];
$minorVersion = $matches["minorVersion"];
$fullVersion = $majorVersion.".".$minorVersion;
}
}
}
if (strpos($userAgent, 'linux')) {
$os = 'linux';
}
else if (strpos($userAgent, 'macintosh') || strpos($userAgent, 'mac os x')) {
$os = 'mac';
}
else if (strpos($userAgent, 'windows') || strpos($userAgent, 'win32')) {
$os = 'windows';
}
return array(
"browser" => $browser,
"majorVersion" => $majorVersion,
"minorVersion" => $minorVersion,
"fullVersion" => $fullVersion,
"os" => $os);
}
?>
HTMLHTMLHTML
//here is the stuff I added
<?php
$browser_inf = getBrowserInfo();
if($browser_inf["browser"] == "firefox" || $browser_inf["browser"] == "mozilla"){
?>
[CSS code that should be sent if the user is using firefox]
<?php }
else {
?>
[code that is sent if user is not using firefox]
<?php } ?>
Please help me fix it?