Style Sheet switcher with Cookies

Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.

Moderator: General Moderators

Post Reply
dyonak
Forum Commoner
Posts: 56
Joined: Wed Jun 22, 2005 10:22 am
Location: Minneapolis, MN
Contact:

Style Sheet switcher with Cookies

Post by dyonak »

This is my first attempt at really working with PHP on my own instead of altering code from my PHP book to suit my purposes. Let me know what you think and any ways to make it more effecient please.

Here's the form to choose between the styles:

Code: Select all

<form action=&quote;index.php&quote; method=&quote;post&quote;>
<input type=&quote;radio&quote; name=&quote;style&quote; value=&quote;http://www.dustinyonak.net/2col_rightNav.css&quote;>Light
<input type=&quote;radio&quote; name=&quote;style&quote; value=&quote;http://www.dustinyonak.net/2col_rightNavblk.css&quote;>Dark
<br />
<input type=&quote;submit&quote; name=&quote;submitstyle&quote; value=&quote;switch style&quote;>
</form>
Here's the form handler:

Code: Select all

<?php

	//Style sheet switcher v0.3b
if (isset ($_POST['submitstyle'])) {
	$_SESSION['style'] = $_POST['style'];
	print "<link rel='stylesheet' 

href='".$_SESSION['style']."' type='text/css' />";
	} else {
	if (isset ($_SESSION['style'])) {
		print "<link rel='stylesheet' 

href='".$_SESSION['style']."' type='text/css' />";
		} else {
		if (isset ($_COOKIE['cookiestyle'])) {
			print "<link rel='stylesheet' 

href='".$_COOKIE['cookiestyle']."' type='text/css' />";
		} else {
		print "<link rel='stylesheet' 

href='http://www.dustinyonak.net/2col_rightNav.css' 

type='text/css' />";
			}
		}
	}
?>
This sets the cookie (~2 months) so the appearance will stay that way after the session is dead.

Code: Select all

<?php
	session_start();
	header("Cache-control: private");
if (isset ($_POST['submitstyle'])) {	
	$_SESSION ['style']= $_POST['style'];
	setcookie('cookiestyle',$_SESSION['style'], time()+5000000);
}
?>
Big thanks to everyone here, especially Burrito, for helping me get started learning PHP! Hopefully someone can get some use out of this.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Here is a cleaner example of your conditional statement.

Code: Select all

if (isset ($_POST['submitstyle']))
	$url = $_POST['submitstyle'];
elseif (isset ($_SESSION['style']))
	$url = $_SESSION['style'];
elseif (isset ($_COOKIE['cookiestyle']))
	$url = $_COOKIE['cookiestyle'];
else
	$url = 'http://www.dustinyonak.net/2col_rightNav.css';

echo '<link rel="stylesheet" href="' . $url . '" type="text/css" />';
dyonak
Forum Commoner
Posts: 56
Joined: Wed Jun 22, 2005 10:22 am
Location: Minneapolis, MN
Contact:

Post by dyonak »

Much cleaner, thanks hawleyjr. One thing though, the:

Code: Select all

$url = $_POST['submitstyle'];
should be:

Code: Select all

$url = $_POST['style'];
Worked great other than that, thanks again.
Post Reply