1. Home
  2. Computing & Technology
  3. PHP / MySQL
photo of Angela Bradley
Angela's PHP / MySQL Blog

By Angela Bradley, About.com Guide to PHP / MySQL

Changing CSS with PHP

Friday April 20, 2007
If your site has more than one "look" you can let people switch between them using a PHP CSS switcher. What this script does is give a drop down listing of the site's available CSS. The user can then switch between them at will.
Comments
April 23, 2007 at 7:15 am
(1) Dom says:

I recently decided that I wanted a stylesheet switcher on my site, but I didn’t want to use Javascript as some people browse with it disabled. So I used PHP as well, although I did it slightly differently.

I had a number of links that the user could select, much like your example, but I used an PHP ‘include’ script to output the link to the stylesheet depending on the value passed through the URL. It, too, checks to see if a value is set, and if not, sets it to a default value.

But the trouble with having variables passed through the URL is that the user could directly type into the address bar a value that hasn’t been catered for in your PHP script. So, in your example, what would happen if the user manually typed into the address bar “changestyle.php?choice=random”? I’m guessing that a stylesheet just wouldn’t be selected at all. The dropdown selector would probably solve that, but if someone used text links (as I did on my site) then there could be a problem.

The advantage of using an ‘include’ script to check the value passed through the URL (and output the appropriate link to the CSS file) is that if the value does not match one of the predefined values, you can set it to default, just as you would if a style was not set in the first place.

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

Explore PHP / MySQL
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. PHP / MySQL

©2009 About.com, a part of The New York Times Company.

All rights reserved.