Jump to content
xisto Community
Sign in to follow this  
iGuest

PHP/CSS Style Switcher

Recommended Posts

This script uses a cookie, so that's your only real restriction, as some browsers wont accept them usually due to privacy/security settings, but most will.

 

 

--------------------------------------------------------------------------------

 

Firstly you'll want to create the themes/styles that you want to use, the more knowledge you have of css the better as your styles need to be mostly css based.

 

Lets say you have two styles style1.css and style2.css now we'll create the file that does the switching between the two.

 

 

Code:<?php if (isset($_POST['set'])) { //set a cookie with the users choice of theme setcookie ('theme',$_POST['set'],time()+31536000,'/','.domain.com','0');             //send the user back to the same page             header("Location: $HTTP_REFERER"); } else {             header("Location: $HTTP_REFERER"); } ?>

 

 

 

Hopefully you can see what to edit "domain.com" is obviously the most important, the rest you can leave as is, 31536000 is the time the cookie you set will last for (in seconds) this is a year, you can edit that if you like.

 

That's basically all the code you need, save it as theme.php and upload, next we need to include the chosen css file into our page.

 

 

 

Code:<link rel="stylesheet" type="text/css" title="user theme" href="http://domain.com/<? echo(!$theme)?'style':$theme ?>.css">  

 

 

Again you need to edit "domain.com" and this should be the full path to your css files, so if they are in a folder called "themes" you would have href="http://forums.xisto.com/no_longer_exists/ etc (don't forget the trailing slash) this will include the chosen theme that's been defined from theme.php

 

Ideally this should be in the <head> section of your page, and obviously in all pages that you want to apply the style to.

 

Only one thing left to do, and that's give your users a way to change the theme, basically all you really need to do is call the following url http://yourdomain.com/theme.php?set=css_file_name (make sure you leave the .css off the end of the css file name)

 

The easiest way for people to select a style would be via a drop down box, something like the following:

 

 

 

Code:  <form name="theme" action="theme.php" method="post"> <select name="set" onChange="this.form.submit()">     <option value="">-- select --</option>     <option value="style1">Style1</option>     <option value="style2">Style2</option> </select> </form>

 

 

 

That's all you need, as the user selects a theme from the list it'll set the $set variable as either style1 or style2, set the cookie with the selected style and call the relevant .css file

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.