PHP Site Skinner

I created some simple PHP to skin or theme my site. The process I employ uses php to set a cookie named "UrlGreySkin" when a user changes the site skin.

On every page

Before the <html> and <doctype> tags
This bit of code has to be included on every page before the html and doctype tags. It checks to see if a skin cookie is set. If one is set, it assigns that value to a variable named $ughskin. If it's not, it assigns a default value to $ughskin. The $ughskin variable is used to insert the correct style sheet for the skin selected.

<?php
// If skin value doesn't exist, assign "lotus" to $ughskin, otherwise assign skin value
(! $_COOKIE["UrlGreySkin"]) ? $ughskin = 'lotus' : $ughskin = $_COOKIE["UrlGreySkin"];
?>

In the <head>
This is where the skinning comes in. I have a directory named css/ which holds the different skin style sheets. Each is named skin-[some skin name].css. Here's an example directory structure:

css/
. skin-babyface.css
. skin-cool.css
. skin-lotus.css
. skin-warm.css

I use php with the style tag to include the correct style sheet for the new setting:

< style type="text/css" title="layout" media="Screen" >
  @import url("/css/skin-<? $ughskin ?>.css");
< /style >

skin.php -- the skin selection page

This page shows all the skins available for the site. The user selects one and the value selected is carried over to skin_response.php, which does the actual cookie setting. Each skin link on skin.php looks something like this:

<a href="/drupal-4-7-2/skin_response.php?skval=lotus&url=<? echo $PHP_SELF; ?>">Pick the Lotus skin</a>

Replace the query string value for "skval" with a new value for each new skin. That value should be the word you used in naming your CSS file. In the above example for a skin named "lotus", I would have a skin file named "skin-lotus.css".

Also note that I pass the current URL to skin_response.php using $PHP_SELF so it can return the user to this page after the skin is set. This is useful if you are going to include a menu on every page of your site, for instance, to change the skin.

skin_response.php -- the php cookie setting page

Finally, skin_response.php deletes any existing skin cookie you may have set and sets a new cookie for 1 year. A confirmation message shows what skin you have selected and links back to the referring page.


<?php
// delete existing cookie
setcookie ("UrlGreySkin", $skval,time() - 3600, "/", ".urlgreyhot.com");
// set new cookie
setcookie ("UrlGreySkin", $skval, time()+(86400*365), "/", ".urlgreyhot.com");
?>

<html>
<body>
<p>Your site theme has been set to "<? echo $skval ?>".</p>
<p align="right"><a class="button" href="/drupal-4-7-2/<? echo $url ?>">OK</a></p>
</body>
</html>