Need a bit of help making php compare two variables

Discussion in 'Web Design and Development' started by joecool85, Oct 27, 2009.

  1. joecool85 macrumors 65816

    joecool85

    Joined:
    Mar 9, 2005
    Location:
    Maine
    #1
    I'm having an issue with a nav bar - it doesn't do drop down for IE6. Well, after 3 hours of playing with it, I found that there isn't a good solution for a drop down menu that uses hover over images and works in IE 6, 7, 8 and current versions of FireFox, Chrome and Safari. My current version works great on everything other than IE 6.

    So, my solution: Keep the current nav bar for all users except IE 6, those guys get a different nav bar loaded.

    So, I need to load inc_ie6_nav.php instead of inc_nav.php - conditional comments didn't like this, evidently they don't work with includes.

    So, I figured I would write a php script to check the browser. Thats where I'm at right now.

    The script below works almost properly and on IE 6 it pulls a value of "MSIE 6.0", it gets all kinds of other randomness with other browsers which is fine. What I want it to do is compare what it finds with the set value of "MSIE 6.0" and if it finds that it is the same, it loads the IE 6 nav bar, if it is different, it loads the normal nav bar.

    However, for whatever reason, the compare portion isn't working. It always returns that it is not the same, even when it is the same.


    PHP:
    <?php

    $string 
    $_SERVER['HTTP_USER_AGENT'];
    $chunks spliti (";"$string);
    $os=$chunks[1];
    $ie6 "MSIE 6.0";

    echo 
    $os.'<br><br>';
    echo 
    $ie6.'<br><br>';

    if (
    $os == $ie6)
    {
    echo 
    "You are using IE 6";
    }
    else {
    echo 
    "Yay, no IE 6!";
    }

    ?>
    I even have it echoing the two items to compare so I can see for myself if it is the same. Indeed, on IE 6 it loads up that $os is "MSIE 6.0" and that $ie6 is "MSIE 6.0" as well.

    Any ideas?
     
  2. SrWebDeveloper macrumors 68000

    SrWebDeveloper

    Joined:
    Dec 7, 2007
    Location:
    Alexandria, VA, USA
    #2
    Here's a function (tested, works) that will do a browser check and report browser information in a convenient associative array with the key names set as 'name', 'version', 'platform' and 'userAgent':

    PHP:
    function BrowserDetect() {

            
    $userAgent strtolower($_SERVER['HTTP_USER_AGENT']);
            
    // Identify the browser. Check Opera and Safari first in case of spoof. Let Google Chrome be identified as Safari.
            
    if (preg_match('/opera/'$userAgent)) {
                
    $name 'opera';
            }
            elseif (
    preg_match('/webkit/'$userAgent)) {
                
    $name 'safari';
            }
            elseif (
    preg_match('/msie/'$userAgent)) {
                
    $name 'msie';
            }
            elseif (
    preg_match('/mozilla/'$userAgent) && !preg_match('/compatible/'$userAgent)) {
                
    $name 'mozilla';
            }
            else {
                
    $name 'unrecognized';
            }
            
    // What version?
            
    if (preg_match('/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/'$userAgent$matches)) {
                
    $version $matches[1];
            }
            else {
                
    $version 'unknown';
            }
            
    // Running on what platform?
            
    if (preg_match('/linux/'$userAgent)) {
                
    $platform 'linux';
            }
            elseif (
    preg_match('/macintosh|mac os x/'$userAgent)) {
                
    $platform 'mac';
            }
            elseif (
    preg_match('/windows|win32/'$userAgent)) {
                
    $platform 'windows';
            }
            else {
                
    $platform 'unrecognized';
            }

            return array(
                
    'name'      => $name,
                
    'version'   => $version,
                
    'platform'  => $platform,
                
    'userAgent' => $userAgent
            
    );
    }
    For example, you might use this simple browser check based on your needs outlined in this topic (edit the conditions in the code below, obviously):

    PHP:
    $browser=BrowserDetect();
    if (
    $browser['name']=='msie' && intval($browser['version'])==6) { ... do whatever ...} else { ... do whatever ... }
    Get the idea?

    -jim
     
  3. joecool85 thread starter macrumors 65816

    joecool85

    Joined:
    Mar 9, 2005
    Location:
    Maine
    #3
    Am I missing something here?

    This is what I tried, and it gives me nothing but blank screen:

    PHP:
    <? php

    function BrowserDetect() {

            
    $userAgent strtolower($_SERVER['HTTP_USER_AGENT']);
            
    // Identify the browser. Check Opera and Safari first in case of spoof. Let Google Chrome be identified as Safari.
            
    if (preg_match('/opera/'$userAgent)) {
                
    $name 'opera';
            }
            elseif (
    preg_match('/webkit/'$userAgent)) {
                
    $name 'safari';
            }
            elseif (
    preg_match('/msie/'$userAgent)) {
                
    $name 'msie';
            }
            elseif (
    preg_match('/mozilla/'$userAgent) && !preg_match('/compatible/'$userAgent)) {
                
    $name 'mozilla';
            }
            else {
                
    $name 'unrecognized';
            }
            
    // What version?
            
    if (preg_match('/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/'$userAgent$matches)) {
                
    $version $matches[1];
            }
            else {
                
    $version 'unknown';
            }
            
    // Running on what platform?
            
    if (preg_match('/linux/'$userAgent)) {
                
    $platform 'linux';
            }
            elseif (
    preg_match('/macintosh|mac os x/'$userAgent)) {
                
    $platform 'mac';
            }
            elseif (
    preg_match('/windows|win32/'$userAgent)) {
                
    $platform 'windows';
            }
            else {
                
    $platform 'unrecognized';
            }

            return array(
                
    'name'      => $name,
                
    'version'   => $version,
                
    'platform'  => $platform,
                
    'userAgent' => $userAgent
            
    );
    }  

    $browser=BrowserDetect();
    if (
    $browser['name']=='msie' && intval($browser['version'])==6) { 
    echo 
    'IE6';
    } else { 
    echo 
    'not IE6';
    }  

    ?>
     
  4. SrWebDeveloper macrumors 68000

    SrWebDeveloper

    Joined:
    Dec 7, 2007
    Location:
    Alexandria, VA, USA
    #4
    You'll laugh when I tell ya.

    PHP:
    <? php 
    Remove that space between the question mark and "php" :cool:

    I used the correct opening and closing tags and copy/pasted your code and worked like a charm. It's always the little things in life!

    Do you use an editor with color highlighting? Helps in situations juuuuuuuuust like this!! I added the space in just for kicks and the color of the tag clearly changed and I got the same as you proving my hypothesis. Many servers allow <? as well as <?php so what happened in your case is PHP saw a dangling "php" not knowing if its a variable or what, and fatal error ensued.

    -jim
     
  5. joecool85 thread starter macrumors 65816

    joecool85

    Joined:
    Mar 9, 2005
    Location:
    Maine
  6. joecool85 thread starter macrumors 65816

    joecool85

    Joined:
    Mar 9, 2005
    Location:
    Maine
    #6
    Your script works great, but my idea doesn't. I can't get it to load a certain include file according to this.

    I'm thinking its not possible to do what I want it to do.
     
  7. SrWebDeveloper macrumors 68000

    SrWebDeveloper

    Joined:
    Dec 7, 2007
    Location:
    Alexandria, VA, USA
    #7
    What you want to do is a simple two step process - step one is identify if the browser is MSIE 6, step two is if true load one kind of navbar, else load another kind.

    I need you code that you use for step two which you have yet to include. Step one is working fine. I can help you if you post the relevant code to see where you're going wrong.

    -jim
     

Share This Page