Lirae.co.uk

  Skip to content?
 

Div Skinning

Skinning with PHP is easy; shove in a bit of code here and a bit of code there, make a couple of different layouts and there you have it: a skinned website.

You must already have some knowledge of using "PHP Includes".
(Skinning is basically the same as using includes, you just have a few extra lines of code, is all.)

Getting Ready

The first thing you need to do is to set up a new folder in your root directory called "skins". Inside "skins", add a numbered folder for each different skin you have; if you have 3 skins, the folders will look like this:

Within each numbered folder, you'll need to have a "header.php", "footer.php", your stylesheet and layout-specific images you'll be using.

Once you've done this, you can upload the folders and move onto the next step.

Coding

First, we'll need to make the file which will control how many skins you have, which skin is the default skin, allow the user to change skins, the path to the header.php & footer.php files, etc.
This file will be called "skins.php" and will be uploaded to your root directory.

<?php
//variables for the skins
$path = "/home/YOURUSERNAME/public_html/skins/";
$header = "/header.php";
$footer = "/footer.php";
$defaultskin = 2;
$totalskins = 3;

//don't edit below this
if(isset($_COOKIE['switchskin']) && (is_numeric($_COOKIE['switchskin']))) {
      $currentskin = strip_tags(stripslashes(trim($_COOKIE['switchskin'])));
}else{
      $currentskin = $defaultskin;
}
$headerfile = "$path$currentskin$header";
$footerfile = "$path$currentskin$footer";

//set the skin
$this_page = trim(strip_tags($_SERVER['PHP_SELF']));
if(isset($_GET['switchskin']) && (is_numeric($_GET['switchskin'])) && ($_GET['switchskin'] < ($totalskins + 1))) {
      setcookie('switchskin',($_GET['switchskin']),time()+(86400*365),'/');
      header("Location: {$this_page}");
}
?>

You'll only need to edit the first chunk of coding:
$path = "/home/YOURUSERNAME/public_html/skins/";
$header = "/header.php";
$footer = "/footer.php";
$defaultskin = 2;
$totalskins = 3;

You will not need to edit anything else in that file.
Reupload the file now. Please. :P

Go through every single page on your site and replace your header include (the one at the top of every page...) with this: <?php
$rootpath = trim(strip_tags($_SERVER['DOCUMENT_ROOT']));
@require("{$rootpath}/skins.php"); @include "$headerfile";
?>
What that basically does is grab the server path and makes sure it's clean, then requires that the "/skins.php" file be included before including the header file.
(If "/skins.php" doesn't exist, the page will stop loading.)

...now replace your footer include (the one at the bottom of every page...) with this code: <?php @include "$footerfile";?> That just includes the footer file. :)

Unless you've screwed up along the way (unlikely since you're most definitely not skim reading this, are you?), everything should work perfectly.

Changing The Skins

There's a reason why "skins.php" is included into every skinned page; the visitor can change the skin with no preview page crap and they won't lose the page they're currently on. Woohoo!

Anyway, the link to change the skin is "?switchskin=[skinnumber]".
Example:
<a href="?switchskin=1">Name Of Skin 1</a>
<a href="?switchskin=2">Name Of Skin 2</a>
<a href="?switchskin=3">Name Of Skin 3</a>
These links can go on any skinned page; the file that allows the visitor to switch the skin is included into every page.

Miscellaneous

You can show the visitor which skin they're currently using by inserting this snippet wherever you want:
You are currently using skin number <?php print $currentskin;?>!

You could also have an image that changes with the skin the person is using:
<img src="http://site.com/skins/<?php print $currentskin;?>/image.gif" alt="short description" />

You could even insert custom layout information in your sidebar (or something):
<?php @include("{$rootpath}/skins/{$currentskin}/info.php");?>

You should be aware though that $_SERVER['DOCUMENT_ROOT'] can be easily spoofed by a nasty visitor (I've also noticed that it refuses to work with AwardSpace.com). If you'd rather be safe than sorry, delete this line: $rootpath = trim(strip_tags($_SERVER['DOCUMENT_ROOT'])); and replace: {$rootpath} with: /home/YOURUSERNAME/public_html You'll need to know your absolute path, though.

 
 
 

All content is copyright © 2004-2008 Carlee Tibbs, unless stated otherwise.

Jump To The Top Of The Page