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
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.