Edmonton Web Design and Photography

Php Switch with a cookie


Posted in: Tutorials, Wordpress on January 6th, 2007

The Php Switch is a neat little script. it can be used for a lot of different things. but today we will create a script that will let a user change what style sheet they want and the script will create a cookie to remember the users input.

we can start off by making a few different style sheets. All i did is changed the background colour, you could change whatever you want with these files.

step 1: the css

body {
	font-family: Arial, Helvetica, sans-serif;
	background-color: #0066CC;
}

a:link, a:active, a:visited {
	font-size: 10px;
	color: #FFFFFF;
	font-family: verdana, Arial, Geneva, Helvetica, sans-serif;
	text-decoration: none;
	font-weight: bold;
}

a:hover {
	font-size: 10px;
	color: #FFFFFF;
	font-family: verdana, Arial, Geneva, Helvetica, sans-serif
}

step 2: understanding a cookie

Now is a good time to have a look at how a basic cookie is setup.

setcookie ('name', value, expire, 'path', 'domain', 'secure');

name – the name of the cookie.
value – the value stored by the cookie on the clients computer.
expire – how long the cookie will stay active, we will use the time() function.
path – the location on the server that the cookie will be stored.
domain – the domain that created the cookie.
secure – if set to true the cookie will be sent over HTTPS otherwise false is all we need.

step 3: setting the cookie

The $HTTP_REFERER will give us the location of the file that sent us to the switcher.php and kick us back there once the cookie has been set. I named the cookie sitestyle, and $set will be the value assigned by our index.php file in a sec, the time on this cookie is in the number of seconds so 31536000 divided by 60 seconds in a min divided by 60 min in a hour divided by 24 hours in a day will give us 365 days until this cookie will expire.

make sure to change the domain to reflect your site.

setcookie ('sitestyle', $set, time()+31536000, '/', 'yourdomain.com', '0');
header("Location: $HTTP_REFERER");

step 4: creating the index file

For the next step we need to make a few links to our CSS files in order to set the values for our cookies. so i went along with the following:

switcher.php?set=red
switcher.php?set=green
switcher.php?set=blue
switcher.php?set=orange

We reference our switcher.php file and use the variable set and assign it the values of out style sheets. In this case red, green, blue, and orange.

Now that the switch is creating a cookie for us and saving what style sheet we want its time to make the code that will read the cookie data and apply the visual change to our page.

echo (!$sitestyle)?'orange':$sitestyle

This chunk of code is basically looking to see if sitestyle has a value, and if not the the default style sheet it will chose will be orange. otherwise its going to take the value of our cookie.

here is the overall index.php code.

<html xmlns="http://www.w3.org/1999/xhtml">
<link rel="stylesheet" type="text/css" media="screen" Defined Style" href="<?php echo (!$sitestyle)?'orange':$sitestyle ?>.css" />

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</meta></head>

<body>
<a href="switcher.php?set=red">red</a>
<a href="switcher.php?set=green">green</a>
<a href="switcher.php?set=blue">blue</a>
<a href="switcher.php?set=orange">orange</a>
</body></link></html>

View a working example of the code we just made here.

Download Source File.
Downloaded 453 Times.

I would appreciate and questions or comments

4 Responses to “ Php Switch with a cookie ”


  1. I would really appreciate some help with this code. It all works fine (as implemented by you above) until I try to use it in sub-directories. I have only tried this in a sub-dir one below (above?) of root and the code only works if I have copies of the .css files in that sub-dir.

    To explain further: I have the switcher.php file and my css files in root and it all works fine. When I make a page in /xxx/ it will not change styles until I put the css files in /xxx/

    Any ideas on how to fix this? An email would be greatly appreciated and a coffee will be certainly coming your way if you can help sort the problem.

    Many thanks.

    Keith.

    Keith Simpson
    February 11, 2008

  2. Yes it would be great for the sub directory problem.

    Alvin
    February 23, 2008

  3. I would appreciate and questions or comments.

    Well, you got a couple there, back in February… did they get a reply?
    The two comments above relate to the same issue – subdirectories. We all try to be “tidy” with our site – who puts .css files in the main directory – surely most users put them in a subdirectory…

    I wonder if you’ll ever get around to responding here.

    ruth
    August 6, 2008

  4. Adam Patterson
    August 7, 2008

Search


Archive


Subscribe