During Christmas time I wanted to present two versions of my site, one being the "Merry Christmas"-like and the other just the regular theme. I wanted the switch to be very, very simple; just a mouse click and done! the CSS is swapped.
I had been looking for a jQuery or one of those "out of the box" scripts, but at the end I turned back to my favored and favorite scripting language, PHP.
The PHP version of how to swap, or switch if you like, a CSS file, is based on a PHP SESSION.
It simply creates a session with a default name ( I'll explain later on ) and then, upon a click, it changes the name of the session which incidentally is the CSS's file name, thus the new file is being loaded and is kept in that session for as long as the user is browsing your site. Trick is done.
First of all you need to initiate a session on EVERY page you're going to use it. And this is very important. ( If you're using a Wordpress' theme you will need to tweak your selected theme's header.php file only ) Add this little piece of code to the very top of every of your pages, right below the opening PHP tag:
<?php
session_start();
This is VERY important, if your script won't work later on, first of all, check IF all the pages have the session opened. That actually is CRUCIAL for the script.
In your INDEX file ( or header.php in Wordpress ) add this little piece of code right below the session opener, so your code would look something like this:
<?php
session_start();
if(!isset($_SESSION['current_style_sheet'])) {
$_SESSION['current_style_sheet'] = 'christmas';
} else {
$_SESSION['current_style_sheet'] = $_SESSION['current_style_sheet'];
}
?>
<html .... .... </html>
where ' Christmas' is the default name of the style sheet.
The above code means nothing but this: IF there is no variable named 'current_style_sheet' create one and name it 'christmas', otherwise use the variable already defined.
Once you have that covered the rest of stuff is pretty simple:
Display a link that will initiate a swapping process with this line:
<a href="swap.php?s=my-file-name-to-switch">My Swap Style</a>
<?php session_start(); $_SESSION['current_style_sheet'] = $_GET['s']; header ( 'location: index.php'; ?>
<?php session_start(); $_SESSION['current_style_sheet'] = $_GET['css_swap']; header ( 'location: index.php'; ?>
and then the very user is kicked back to the main site, unnoticedly.
One last thing you need to tweak: your META tags, to invite the current style ( which is already stored in PHP's session ) to every of your page.
<link href="<?php echo $_SESSION['current_style_sheet']; ?>.css" rel="stylesheet" />
Done.
Note: gregbialowas.com is using a slightly different method of redirecting users yet you should get an idea of how to do that on your own website.
Note: The above version wasn't tested so if you have questions, don't hesitate to address them at: i at gregbialowas.com
Summary: Font-end and Back-end developer, also designer with 25+ years of experience. Currently based in Poland. Eligible to work in the US (US Social Security Number holder) for any employer on W2 / 1099 basis. Utilizes both technical skills and designing aptitude. Lived and worked in Europe, Australia and North America.
PHP CMS HTML5 CSS3 RWD OOP MySQL PDO JS jQuery JSON GIT Bitbucket GitHub Gulp
I've implemented language version feature, ready for additional languages, based on URL modification. Doesn't rely on cookies or sessions and is available via a /{lang} modifier. More about languages
Zaimplementowałem wersję językową, gotową na dodanie kolejnych języków, opartą na modyfikacji URL, która nie korzysta z plików cookie ani sesji. Wersja językowa dostępna jest przez modyfikator /{język}. Więcej o językach