Want to have a style switcher that lets your site’s visitors choose a different style sheet? Want it to work even if there is no JavaScript support? The trick is to use a server-side language like PHP, which is what I use for my style switcher.
Using PHP to let the user switch to a different CSS file is nothing new. But it is one of the things that I am often asked about, so I thought it would be good to have a write-up to refer people to in the future.

As I mentioned this will work when JavaScript is disabled. It will not work when cookies are disabled, however. Nothing bad will happen, but it simply won’t work and the user will be returned to the same page they were on. Just to make you aware of that right from the start.

I use this style switcher to let visitors switch between two different layouts (“Zoom” and “Normal”) here on 456 Berea Street. If you want to let the user choose between more than two stylesheets you will need to come up with a different solution.

I wouldn’t call myself a PHP expert by any means, so if you think my code could be improved in any way, please let me know.

To switch styles, all you need to do is activate the link to the styleswitcher. As you may have noticed if you have tried that, the URL that invokes the styleswitcher is always the same: /styleswitch/. The link leads to a php file that looks like this:

  1. <?php
  2. $layout = (isset($_COOKIE['layout']) && ($_COOKIE['layout'] == "zoom")) ? "main" : "zoom";
  3. setcookie("layout", $layout, time()+31536000, '/');
  4. $ref = (isset($_SERVER['HTTP_REFERER'])) ? $_SERVER['HTTP_REFERER'] : "http://{$_SERVER['SERVER_NAME']}/";
  5. header("Location: $ref");
  6. ?>
Here’s what happens, line by line:

  • Line 2: If the layout cookie exists (the browser has been here before) and has the value “zoom” (the user has switched to Zoom layout), $layout is set to “main”. Otherwise $layout is set to “zoom” (the browser doesn’t accept cookies, the style switcher has not been used with this browser, or the user has switched to Normal layout). Note that “main” and “zoom” are the file names (except for the .css extension) used for the respective styles. Change these values to match your file names.
  • Line 3: The value of $layout is stored in a cookie.
  • Line 4: If a referer header exists, its value is stored in $ref. If it doesn’t exist, $ref is set to “http://{$_SERVER[‘SERVER_NAME’]}/” as a backup to redirect the browser to the home page.
  • Line 5: The browser is redirected to the URL stored in $ref.
On all pages a check is performed to decide which stylesheet to load:

  1. <?php
  2. $layout = (isset($_COOKIE['layout']) && ($_COOKIE['layout'] == "zoom")) ? "zoom" : "main";
  3. ?>
If a cookie exists and its value is “zoom”, $layout is set to “zoom”. Otherwise, $layout is set to “main”. At this point $layout contains the name of the CSS file that should be loaded.

Finally, when loading the main style sheet the value stored in $layout is inserted instead of the file name in the @import path:

  1. <style type="text/css" media="screen,projection">
  2. @import '/css/<?php echo $layout; ?>.css';
  3. </style>
And that’s it. Use it, spread it, improve it.
Thursday, March 11, 2010

Automated News Aggregator: PHP RSS Reader

PHP RSS Reader is an application which can crawl an unlimited number of feeds & presents them with an easy-to-use interface.
For an organized navigation, It is possible to create categories & place feeds inside them.
A handy feature is "keyword-based" categories which can auto-move items with the keywords mentioned into the related categories which is great for anyone who follows multiple feeds but hunts for specific keywords.


PHP RSS Reader comes with a complete admin panel where every setting can be controlled.
The application creates SEO-friendly URLs for every category & article which is important for anyone who will use PHP RSS Reader for creating a feed-based website rather than using it as a personal feed-reader.
It is possible to change the look & feel of the interface by simply playing with the CSS files.
There is a web-based installer that help making the application online in a few minutes.
PHP RSS Reader comes with 2 versions:
The application is a must-check for anyone looking for an advanced RSS reader, planning to create a feed-based website or integrate feeds into an application (where PHP RSS Reader can be used only as the backend engine).
P.S. This post is a sponsored review.

chive is an open source & web-based MySQL management application that is built with PHP.
It can achieve the most common tasks like creating, editing & executing:
  • databases
  • tables
  • indices, keys, triggers, views, routines
  • privileges management
  • import/export


The application has a built-in editor with syntax-highlighting which is nice when running complex queries & an easy-to-use interface that makes browsing fast.
chive has one major fallback (which may pr may not effect you): it only supports MySQL 5+.