example.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>PHP Switcher Example</title>
<!-- Dynamic stylesheet -->
<link rel="stylesheet" href="<?php
if(isset($_COOKIE['sitestyle'])){
print trim($_COOKIE['sitestyle']);
}
else {
print "normal";
}
?>.css" media="screen" />
<link rel="alternate stylesheet" href="normal.css" media="screen" title="Default Style" />
<link rel="alternate stylesheet" href="different.css" media="screen" title="Different Stylesheet" />
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-1853623-1";
urchinTracker();
</script>
</head>
<body>
<h1>PHP Styleswitcher Example</h1>
<p><a href="switcher.php?set=different">Different stylesheet</a><br />
<a href="switcher.php?set=normal">Normal stylesheet</a></p>
<p><a href="index.php">Back</a></p>
<p>Copyright © 2002 <a href="http://www.contrastsweb.com/">Rob Ballou</a></p>
</body>
</html>
switcher.php
<?php
// This array lists the "acceptable" styles
$accept = array('normal',
'different');
// Get style from a query string (e.g. from a link),
// or from a form.
if(isset($_REQUEST['set'])){
$style = trim(strip_tags($_REQUEST['set']));
}
else if(isset($_POST['set'])){
$style = trim(strip_tags($_POST['set']));
}
// Check if the requested stylesheet is "acceptable"
if(in_array($style, $accept)){
setcookie("sitestyle", $style, time()+31536000, '/switcher/', 'contrastsweb.com', '0');
}
if(isset($_SERVER['HTTP_REFERER'])){
// Send the user back to the refering page
header("Location: ". $_SERVER['HTTP_REFERER']);
exit;
}
else {
// No HTTP referrer, send them back to the home page
header("Location: http://contrastsweb.com/switcher/example.php");
exit;
}
?>