Fast CSS Style Switching with PHP

Published in CSS on Thursday, March 11th, 2004

Updated: New content added 12/03/2004.

Here's a little something that has come in useful for me on a few occasions.

It seems to be inevitable that at some point along the life of a project, someone from your clients company decides that they don't like the <insert style element here /> of their webpage, and they want to 'just quickly' see it in another color, position or style.

In many situations, this can be a little troublesome, however with a little PHP you can solve this problem quickly and easily.

The demand

When a client begins the 'hmm... what if we do this and this?' phase late in the development cycle, what is needed is an unobtrusive way to show them what their site would look like with their suggestions.

The result - A quick PHP-CSS style switcher

Basically, rather than including your css stylesheet directly in your html, you use PHP and an optional GET request to serve up a stylesheet. So, with an url like example 1 below, the site would be served with it's default style, whereas in example 2 it would be served with the specified alternate stylesheet.

  1. http://www.mydomain.com
  2. http://www.mydomain.com/?css=/c/alternatestyle.css

    (note: in this example I am specifying a directory location and the name of the stylesheet.)

Examples in action

A little stroll thru the Zen Garden and you can see a method similar to this in action.

Here's the code

This is the function:

	
	function css(){
	if(isset($_GET['css']))
		{ 
		$style = $_GET['css'];
		}
	else $style = "/css/thestylefile.css"; 
	
	echo $style;
	}

This is how you declare your stylesheet:

	<style type="text/css"><!--
	@import url(<?php css() ?>)
	-->;</style>
	

Or:

	<link href="<?php css() ?>"
	rel="stylesheet" type="text/css">

The breakdown

Here's how the function works:

  1. If the GET parameter 'css' is present, set the variable 'style' equal to the value of that for the GET parameter.
  2. If the GET parameter 'css' is not present, set the variable 'style' equal to your default css file.

Now you can simply send your client a link to the two different urls, and they will be able to see the two different styles.

Works on any page in the site

Provided that this function is used to set the stylesheet on all of the pages of your site, the '?css=/c/alternatestyle.css' can be appended to the end of any url within that site to swap sheets.

Why would you need this? You likely have different types of pages within the site; if your client has enough 'web savvy' they can go ahead and paste that bit of text to the end of the url of any page they choose to examine.

Comments and Feedback

Of course, this can be done easily in other languages too. In old-style JSP (which, like PHP, puts Java code straight into the page), it would look like this:


<%
String cssFile = request.getParameter("css");
if (cssFile == null || "".equals(cssFile)) {
cssFile = /css/thestylefile.css;
}
%>
<style type="text/css"><!--
@import url(<%= cssFile %>)
-->;</style>

By the way, in your PHP example, wouldn't it be better practice to return the filename rather than echo it out directly?

Sorry I couldn't get the code to line up - the pre tag wasn't allowed. Maybe you could add a white-space:pre to your CSS for the code tag?

Hey Jennifer, thanks for the added input!

With regards to 'better practice' you may just be the person to tell me why; perhaps it's obvious, but I've often wondered about that, and do not have the background education or experience to know any better ;-]

Embarrasingly(?), I tend to use return when I need something more than echo.

With regards to posting code, that definitely needs some work. I have an upgrade scheduled for this weekend, so I'll put that on the list.

Well, I think returning a value from the function instead of using echo would make the function more flexible and reusable. For example, you might have another function that might want to call your css() function and manipulate the filename somehow, or use the filename in a call to yet another function.

You may not need that sort of functionality right now, but it makes it easy to reuse it in different ways in the future.

Also, it should be pretty easy to combine this technique with a radio button in your HTML:


<input type="radio" name="css" value="/css/styles1.css"/> First stylesheet
<input type="radio" name="css" value="/css/styles2.css"/> Second stylesheet
<input type="radio" name="css" value="/css/styles3.css"/> Third stylesheet
<input type="submit" value="Switch styles"/>

Why not set a cookie with selected stylesheet to make user's choice more or less permanent?

Change if(isset($_GET['css'])) .. to if(isset($_GET['css']) || isset($_COOKIE['css'])) { $style = $_REQUEST['css']; setcookie('css', $style); }

By the way: Would be nice to check via file_exists() if the desired stylesheet defined in your GET-params is really there :-)

Thanks for the comments you two!

  1. Jennifer, I can see how using return is definitely more flexible.
  2. With regards to the radio button and the cookie, note that this wasn't intended as a complete end-user 'style switcher', but rather an unobtrusive way to show a client what their site would look like when they begin the 'hmm... what if we do this and this?' phase late in the development cycle.

Maybe I'll rewrite this including your suggestions and note the different possibilities... First though I may go and update the 'purpose' of this entry...

Though PHP is indeed very handy for things like this, always validate your input... last thing we want is a compromised and defaced server as some smart arse started injecting HTML/PHP/SQL via "GET".

Just a simple change:


function css(){
$style = htmlspecialchars($_GET['css']);
if(@file_exists('a/path/prefix' . $style)) {
$style = $_GET['css'];
}
else
$style = '/css/thestylefile.css';

echo $style;
}

We're not really bothered if the variable is set, we're more concerned if the one that is set hasn't got escaped nasties like single or double quotes and also if the file actually exists on the filesystem, where 'a/path/prefix' is whatever actual path is needed to get to '/css/thestylefile.css', say '/home/user/htdocs'.

A PHP based styleswitcher has been a permanent feature of my site for a while... decided to use cookies over PHP sessions though. :D

Thanks Jonathan, another great bit of input. I had not bothered with this as I had intended it more as a quick tool for the developer, rather than as a dedicated style switcher; with the comments here though I would imagine one could be cobbled together.

This quick search for php style switcher may provide some more complete examples for building a dedicated style switcher...

Been there, done that, bought shirt. :P Very unobtrusive too.

Hey Raena, cool favicon! And a great tutorial you have there, though not what I am trying to get at here.

Let me just stress this one more time: this is a quick and dirty style switcher, not intended for permanent end-user use!

No different then the example site I gave above. Quick and simple, no cookie required, in fact, in this case it's not wanted.

You see, this was inspired when a client called up and said to me: "Hey Mike, remember at the meeting when we tried out that different header and those other shades of grey-blue in the background? Can you put up those examples for the others here at the office to see?"

I didn't have time to monkey too much with the site on the testing server, nor did I want to implement anything cookie based, because they just wanted a quick look at what we had done. That is what this is for :-]

What's far more effektive for webdevelopers, is semi-automatic-generating of CSS via PHP:

$css["color"] ="black";

$css["background-site"]="#D6CFC4";

$file_css = fopen ("test.css", "w+");

fwrite ($file_css, "html {font-size:x-small; font-weight:normal;

background-color:".$css["background-site"]."; color:".$css["maincolor"]."; }n");

fclose($file_css) ;

So you can define central-formats like colors, fonts, sizes in an array and add them dynamically in the css.

You could even do something like this:

$css["mode"][0]["color"] ="#F7941D";//CMYK 0/50/100/0

$css["mode"][0]["backgroundcolor"] ="#FCE4C7";

$css["mode"][0]["subbordercolor"] ="#FFD182";

$css["mode"][1]["color"] ="#F37021";//CMYK 0/70/100/0

$css["mode"][1]["backgroundcolor"] ="#F8DECE";

$css["mode"][1]["subbordercolor"] ="#F5A270";

$file_css = fopen ("test.css", "w+");

$counter=0;

while($conf["mode"][$counter]["color"]!=""){

fwrite ($file_css, "em.mode".$counter." n");

fwrite ($file_css, "#content-mode".$counter." em n");

$counter++;

}

fclose($file_css) ;

[I'm sorry for the empty "p"-elements in my comment, but the pre-element is not allowed here. How else should I markup an empty line ?]

Neat Idea Ben! Sorry about the 'pre' thing. I'm planning on upgrading the whole blog script over the weekend, and that has been one of the most major complaints so far...

Mike, I have to agree that returning values from functions is better than echoing within functions. Why?
Lots of stylistic reasons, but in addition, let's assume that you are writing spaghetti code. You definitely want to assign the return value of the function to a variable and then plug the var into a template later on. If not, what if you decide later on your page that you need to send some http headers? Death.

Home » Blog » Web Development » CSS

Check out the blog categories for older content

The latest from my personal website,
Mike Papageorge.com

SiteUptime Web Site Monitoring Service

Sitepoint's web devlopment books have helped me out on many occasions both for finding a quick solution to a problem but also to level out my knowlegde in weaker areas (JavaScript, I'm looking at you!). I am recommending the following titles from my bookshelf:

The Principles Of Successful Freelancing

I started freelancing by diving in head first and getting on with it. Many years and a lot of experience later I was still able to take away some gems from this book, and there are plenty I wish I had thought of beforehand. If you are new to freelancing and have a lot of questions (or maybe don't know what questions to ask!) do yourself a favor and at least check out the sample chapters.

The Art & Science Of JavaScript

The author line-up for this book says it all. 7 excellent developers show you how to get your JavaScript coding up to speed with 7 chapters of great theory, code and examples. Metaprogramming with JavaScript (chapter 5 from Dan Webb) really helped me iron out some things I was missing about JavaScript. That said each chapter really helped me to develop my JavaScript skills beyond simple Ajax calls and html insertion with libs like JQuery.

The PHP Anthology: 101 Essential Tips, Tricks & Hacks

Like the other books listed here, this provides a great reference for the PHP developer looking to have the right answers from the right people at their fingertips. I tend to pull this off the shelf when I need to delve into new territory and usually find a workable solution to keep development moving. This only needs to happen once and you recoup the price of the book in time saved from having to develop the solution or find the right pattern for getting the job done..