Browser compatibility issue using php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Chis
Forum Newbie
Posts: 3
Joined: Sun Sep 27, 2009 6:41 pm

Browser compatibility issue using php

Post 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!
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Browser compatibility issue using php

Post 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.
Chis
Forum Newbie
Posts: 3
Joined: Sun Sep 27, 2009 6:41 pm

Re: Browser compatibility issue using php

Post 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?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Browser compatibility issue using php

Post 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.
Chis
Forum Newbie
Posts: 3
Joined: Sun Sep 27, 2009 6:41 pm

Re: Browser compatibility issue using php

Post 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?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Browser compatibility issue using php

Post 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.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Browser compatibility issue using php

Post 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.
Post Reply