Hello,

 I am trying my best to figure the browser detection code I found in the article on this site from 2001 or 02.. I think I have it updated to include Firefox which was the only major browser not listed. 

 What I am trying to do is use the browser detect script to determine which browser is being used for the sake of determining which stylesheet file to link to. I am looking for suggestions of a.) an easier method to do this, or b.) how to make where I am going with this work properly.

So, what I have done is put several <link>s to the different css files for which browser the user is using. I am painstakingly coding the stylsheets for each browser, then displaying them in the browser after updates to make sure they work. Here is what I have:

handle_form.php

<html>
<head>
<title>REAP</title>
<link rel="stylesheet" type="text/css" href="styles_ff.css" />
<link rel="stylesheet" type="text/css" href="styles_op.css" />
<link rel="stylesheet" type="text/css" href="styles_ns.css" />
<link rel="stylesheet" type="text/css" href="styles_ie.css" />
</head>
<body>
</head>
<body>
<?php
require ('browser.php');

browser.php

<?php

unset ($BROWSER_AGENT);
unset ($BROWSER_VER);
unset ($BROWSER_PLATFORM);

function browser_get_agent () {
  global $BROWSER_AGENT;
  return $BROWSER_AGENT;
}

function browser_get_version() {
  global $BROWSER_VER;
  return $BROWSER_VER;
}

function browser_get_platform() {
  global $BROWSER_PLATFORM;
  return $BROWSER_PLATFORM;
}

function browser_is_mac() {
  if (browser_get_platform()=='Mac') {
    return true;
  } else {
    return false;
  }
}

function browser_is_windows() {
  if (browser_get_platform()=='Win') {
    return true;
  } else {
    return false;
  }
}

function browser_is_ie() {
  if (browser_get_agent()=='IE') {
    return "Internet Explorer";
  } else {
    return false;
  }
}

function browser_is_netscape() {
  if (browser_get_agent()=='MOZILLA') {
    return "Netscape";
  } else {
    return false;
  }
}

function browser_is_firefox() {   //added this
  if (browser_get_agent()=='MOZILLA') {
    return "Firefox";
  } else {
    return false;
  }
}

function browser_is_opera() {    //added this 
  if (browser_get_agent()=='OPERA') {
    return "Opera";
  } else {
    return false;
  }
}


/* Determine browser and version */


if (ereg( 'MSIE ([0-9].[0-9]{1,2})', $_SERVER['HTTP_USER_AGENT'], $log_version)) {
  $BROWSER_VER=$log_version[1];
  $BROWSER_AGENT='IE';
} elseif (ereg( 'Opera ([0-9].[0-9]{1,2})', $_SERVER['HTTP_USER_AGENT'], $log_version)) {
  $BROWSER_VER=$log_version[1];
  $BROWSER_AGENT='OPERA';
} elseif (ereg( 'Mozilla/([0-9].[0-9]{1,2})', $_SERVER['HTTP_USER_AGENT'], $log_version)) {
  $BROWSER_VER=$log_version[1];
  $BROWSER_AGENT='MOZILLA';
} elseif (ereg( 'FIREFOX ([0-9].[0-9]{1,2})', $_SERVER['HTTP_USER_AGENT'],  // personally added this code$log_version)) {
  $BROWSER_VER=$log_version[1];
  $BROWSER_AGENT='FIREFOX';
} else {
  $BROWSER_VER=0;
  $BROWSER_AGENT='OTHER';
}

/*
  Determine platform
*/

if (strstr($_SERVER['HTTP_USER_AGENT'],'Win')) {
  $BROWSER_PLATFORM='Win';
} else if (strstr($_SERVER['HTTP_USER_AGENT'],'Mac')) {
  $BROWSER_PLATFORM='Mac';
} else if (strstr($_SERVER['HTTP_USER_AGENT'],'Linux')) {
  $BROWSER_PLATFORM='Linux';
} else if (strstr($_SERVER['HTTP_USER_AGENT'],'Unix')) {
  $BROWSER_PLATFORM='Unix';
} else {
  $BROWSER_PLATFORM='Other';
}

/* THIS BROWSER VERSION IS LESS THAN 5 DO WHAT YOU WANT 
if($BROWSER_VER <= '5'){
    echo "What ever you want the script to do here"; */
}
?> 

I know the code works as I tested it, but how to get the code to work to determine which stylesheet to load for me is another story.

my suggestion

<?php
//something like this is what I had in mind
if ($BROWSER_AGENT == 'IE'){
    <link rel="stylesheet" type="text/css" href="styles_ie.css" />;  //obviously needing a variable assigned to it
} elseif   ($BROWSER_AGENT == 'FIREFOX'){
    <link rel="stylesheet" type="text/css" href="styles_ff.css" />;
}
?>
 I just don't quite understand how the <head> tag works with php and if you can assign variables to it to change it etc. I hope this makes sense. Thanks for the help as you all are always quick and very helpful!

Warmaster

    If you write w3c-standards-compliant HTML and CSS and ensure that the first line of your HTML output is a fully qualified doctype declaration (which will get IE out of "quirks mode"), chances are you will not need to worry about which browser is accessing your site (unless it's a really old browser/version). If there is a problem, most likely it will be some version of MS IE that is wrong. For those cases (hopefully quite rare once you take care of the valid mark-up/CSS and doctype issues), you can use IE conditional comments to download an IE-only stylesheet with selector/properties intended to override or add to those in the standard stylesheet.

    <link rel='stylesheet' href='styles.css' type='text/css'>
    <!--[if ie]>
    <link rel='stylesheet' href='styles_ie.css' type='text/css'>
    <![endif]-->
    

      Thanks NogDog, I checked out that validator and I had all kinds of errors so I am just going to re-write my page using styles completely and get rid of the stupid tables. I always hated tables. It looks good but to get the table I have to work, the coding is all messed up closing td's in places they should be closed, but when I close it in the right spot it messes the table up.

      I knew it was messed up but it was working so I left it, and now I just have to redo it. Oh well, such is life. A good learning experience for me lol. Of course, I also did not have a proper doctype cause I was mixing xhtml and html lesson learned there too.

      Warmaster

        We also have the issue
        that most any browser can be telling it is another one.
        Most browser and other software for the web have a setting for this.
        When you run firefox you can present yourself as being an IE.

          Thanks for the replies...I will use the IE if conditionals and test again...after I get done re-writing my forms. I guess if it were easy then everyone would be doing this haha.

          Warmaster

            Ok, I did some testing using the if conditionals and found that they worked great for ie and firefox. However, I ran into problems with Opera. Opera does not render the same as Firefox and the if conditionals for the html comment are limited so I could not put a condition in there to test load a style sheet for Opera. Regardless, I came up with my own solution and figured I could put it in here to get some feedback to see if this is a viable solution or if there is something wrong with the way I am doing it.

            I first did this:

            <?php
            $browser_check = $BROWSER_AGENT;
            	if ($browser_check == 'IE'){
            		$opera = header('Location: handle_form_ie.php');
            	}elseif ($browser_check == 'OPERA'){
            		$opera = header('Location: handle_form_op.php');
            	}elseif ($browser_check == 'FIREFOX'){
            		$firefox = header('Location: handle_form.php');
            	} 
            /* adding support for Netscape later */
            ?>
            

            Here is my new solution and I am open to critique. I ended up using the browser check software listed above to make this work for me with one slight problem...

            <!DOCTYPE HTML PUBLIC "-/*W3C/*DTD HTML 4.01 Transitional/*EN">
            <html>
            <?php
            /* Checking which browser is being used */
            require ('browser.php');
            
            /* Normal header (Firefox and Mozilla)*/
            $head = '<head>
            <meta http-equiv="content-type" content="text/html; charset= iso-8859-1" />
            <title>REAP</title>
            <link rel=\'stylesheet\' href=\'styles.css\' type=\'text/css\'>
            </head>';
            
            /* IE header */
            $head_ie = '<head>
            <meta http-equiv="content-type" content="text/html; charset= iso-8859-1" />
            <title>REAP</title>
            <link rel=\'stylesheet\' href=\'styles_ie.css\' type=\'text/css\'>
            </head>';
            
            /* Opera header */
            $head_op = '<head>
            <meta http-equiv="content-type" content="text/html; charset= iso-8859-1" />
            <title>REAP</title>
            <link rel=\'stylesheet\' href=\'styles_op.css\' type=\'text/css\'>
            </head>';
            
            $body = '<body>';
            	if ($BROWSER_AGENT == 'IE'){
            		$head = $head_ie;
            	}elseif ($BROWSER_AGENT == 'OTHER'){
            		$head = $head_op;
            	}elseif ($BROWSER_AGENT == 'FIREFOX'){
            		$head = $head;
            	}
            echo $head;
            echo $body;
            
            /* Checking functionality in browsers */
            echo "<li>$BROWSER_AGENT</li><li>$BROWSER_VER</li><li>$BROWSER_PLATFORM</li>";
            ?>
            

            This works great for IE and Firefox but again, Opera gives me troubles... (Beginning to think I don't like Opera). HTTP_USER_AGENT detects IE and Firefox, but reports "OTHER" Version "0" for Opera. As a stupid quick fix, I just listed "OTHER" to use the Opera CSS. It works, but is not what I had in mind. So, anyone have a solution to this problem?

            btw, I used HTML Tidy to clean my code for me and I ended up not re-writing the scrip yay! Thanks for the links there NogDog, saved me time and I have no errors/warnings!

              Write a Reply...