Page 1 of 1

Browser compatibility issue using php

Posted: Sun Sep 27, 2009 7:06 pm
by Chis
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.

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 } ?>
 
I'm not sure what the heck I put in, but I'm pretty sure it's dead wrong.
Please help me fix it? :P Thanks!

Re: Browser compatibility issue using php

Posted: Sun Sep 27, 2009 9:03 pm
by Ollie Saunders
Put var_dump($browser_inf["browser"]) above your if. I reckon, the only reason the equality is failing consistently is down to capitalization.

Re: Browser compatibility issue using php

Posted: Sun Sep 27, 2009 9:19 pm
by Chis
Thanks for the input. Where should I change my capitalization? It looks like all the browser info is in lowercase.
I tried inserting var_dump($browser_inf["browser"]) and now it says string(11) "unsupported" at the top. Does that mean $browser never changed or didn't recognize Firefox for some reason?

Re: Browser compatibility issue using php

Posted: Mon Sep 28, 2009 4:05 am
by Ollie Saunders
Forget about capitalization, looks like I was wrong.
Does that mean $browser never changed or didn't recognize Firefox for some reason?
I'd say so, yes.

Try get_browser(), instead; ensure you read the note at the bottom the page, after following that link.

Re: Browser compatibility issue using php

Posted: Mon Sep 28, 2009 9:31 am
by Chis
To be honest, I actually have no idea where my browscap.ini file is located, or if I have one at all. It also says that it's about 300KB to load.
But in the example in that link, it seems that I can just derive the browser information from $_SERVER['HTTP_USER_AGENT'] ... ? Could I just check if the word Firefox exists as a substring within that variable or something?

Re: Browser compatibility issue using php

Posted: Mon Sep 28, 2009 10:49 am
by Ollie Saunders
Uh, yeah, that would probably work. Although, I think what you want to look for is "Gecko", which is the name of the Firefox engine, or something. I'm not sure the word "Firefox" actually appears in Firefox's user agent string.

Re: Browser compatibility issue using php

Posted: Mon Sep 28, 2009 10:57 am
by Eran
You shouldn't be relying on PHP or user agent detection to fix the layout of your site, that's very unreliable. Use CSS only, there are plenty of ways to target specific browsers.