<?php
// choosestyle.php - Grant Root - 3/10/03
//
// This snippet of code demonstrates how to choose and link to an appropriate
// stylesheet for a site based on the user agent (browser). It uses PHP's
// get_browser function, and caches the resultant data using a session variable
// so that the function is not called for every page access.
//
// Copyright 2003 Grant Root (grant@rootcentral.org)
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details (www.gnu.org).


// We use sessions in order to avoid calling get_browser for every page in the
// site, since it adds considerable overhead. 

session_start();  // Must be placed before any output to the browser

if (session_is_registered('browser'))  // See if the browser info is cached
{
   $browser = $_SESSION['browser'];
}
else
{
   // Get browser information using browscap.ini
   // (Note: This feature must be enabled in php.ini, and a copy of
   // browscap.ini musr be obtained and installed. See the function description
   // for get_browser for more details.)
   $browser = get_browser();

   $_SESSION['browser'] = $browser;  // Cache browser info
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<!-- Browser detected: <?php echo $browser->browser; ?>,
     Version: <?php echo $browser->version; ?> -->

<?php if ($browser->browser == "IE" && $browser->version < 4) : ?>
<!-- IE 3 (or older) detected, no stylesheet sent -->
<?php elseif ($browser->browser == "IE" && $browser->version < 6) : ?>
<link rel="stylesheet" href="/styles/ie5.css" type="text/css" />
<?php elseif ($browser->browser == "Netscape" && $browser->version < 6) : ?>
<link rel="stylesheet" href="/styles/ns4.css" type="text/css" />
<?php else :  // Send default stylesheet for CSS-compliant browsers ?>
<link rel="stylesheet" href="/styles/main.css" type="text/css" />
<?php endif; ?>

<title>My Page</title>
</head>
