View Full Version : allowing viewers to change stylesheets with php

06-19-2006, 07:50 AM
ok, so i remember having seen a lil snippet of code somewhere in the past that allowed the viewer of the site to click a link, and it would change the stylesheet which was being used for the site. giving the site a sort of skinnable function to it. i was wondering how i would go about doing something like this. i would also like the choice to stick with the user by applying a cookie or something like that. so on return visits their choic eof skin will remain.

06-19-2006, 08:48 AM
Well, you'd have a bunch of links e.g.

<a href="stylechange.php?style=s1">Style 1</a> <a href="stylechange.php?style=s2">Style 2</a> <a href="stylechange.php?style=s3">Style 3</a>

Then, in your stylechange.php you'd have something like...

$style = $_GET['style'];
setcookie('style', $style, time()+604800);
header("Location: index.php");

Then, instead of your usual style include line, you'd have something like...

if (!isset($HTTP_COOKIE_VARS['style'])) {
echo '<link rel="stylesheet" href="defaultstyle.css" type="text/css" />';
else {
echo '<link rel="stylesheet" href="' . $HTTP_COOKIE_VARS['style'] . '.css" type="text/css" />';

So, basically, all you'd do is:

Add the above code
Modify the header("Location: index.php"); to go to the page you choose once the style cookie has been set
Name all your style sheets the same as your s e.g. in stylechange.php?style=s2, the sheet would be named s2.css
Put as many links as you need


03-07-2009, 03:37 PM
instead of directing it to the index.php, i had the code of stylechange.php on top of my page. it didn't work for me :confused:

also want to know what does time()+604800 do?????