Advanced Cookies multiple properties+values in single cookie
Posted: Thu Apr 24, 2008 12:56 am
My site currently sets a lot of cookies, and then there are the third party scripts I use (such as the blog) that set cookies. I've got so many cookies now I'm surprised my site isn't crawling with ants. 
Any way I figure it's now or never to consolidate the cookies that I have personally constructed in the site. I'm not worried about third party cookies as there are perhaps half a dozen if you find all of them. However I'm certainly starting to push the client's maximum allowed number of cookies but I'm certainly not utilizing the allowed space in the cookies with single digit values (typically 0, 1, or 2).
So essentially I'd like to merge most of the cookies such as the following...
audio
backgroundimages
chatroom
connection
counter
css3
cursors
dhtmleffects
initialfocus
...and then I'd like to have the cookie sent to the client look something like this (I'll just use a comma as a separator between "sets" of what cookies I just listed above as)...
Desired value example of cookie Name: 'settings', merged from the cookies above...
audio=1,backgroundimages=1,chatroom=0,connection=2,counter=43,css3=1,cursors=0,dhtmleffects=1,initialfocus=search
113 bytes out of 4KB is well within the allowed limits (from what I have read).
With a couple of weeks wandering blind-folded on the internet I could probably construct something that would create a thermal nuclear meltdown of the server though I think it's best that I consult with those who have more experience with magic then voodoo.
I currently handle setting cookies in my PHP class file where I handle the creation of all the PHP classes that I use.
Here is essentially what my class file looks like with a two examples of settings that visitors can manipulate on my site...
I'm not sure what I will need though I am thinking I'll end up using an array and/or a for each loop. I'd prefer if it's within the realm of low-load sanity to replace the setcookie method with say a variable or something to later construct the cookie in the PHP class file obviously as it would minimize having to completely annihilate my works-for-me PHP class file. Also two examples of property/value sets (I'm using CSS terminology here but the property is what will be set (such as audio, CSS3, connection) and the value it's, well value) would help minimize confusion (making it clear what is unique and what I have if at all duplicate for each property/value set).
I hope all of this makes sense! So how should I start approaching this?
Any way I figure it's now or never to consolidate the cookies that I have personally constructed in the site. I'm not worried about third party cookies as there are perhaps half a dozen if you find all of them. However I'm certainly starting to push the client's maximum allowed number of cookies but I'm certainly not utilizing the allowed space in the cookies with single digit values (typically 0, 1, or 2).
So essentially I'd like to merge most of the cookies such as the following...
audio
backgroundimages
chatroom
connection
counter
css3
cursors
dhtmleffects
initialfocus
...and then I'd like to have the cookie sent to the client look something like this (I'll just use a comma as a separator between "sets" of what cookies I just listed above as)...
Desired value example of cookie Name: 'settings', merged from the cookies above...
audio=1,backgroundimages=1,chatroom=0,connection=2,counter=43,css3=1,cursors=0,dhtmleffects=1,initialfocus=search
113 bytes out of 4KB is well within the allowed limits (from what I have read).
With a couple of weeks wandering blind-folded on the internet I could probably construct something that would create a thermal nuclear meltdown of the server though I think it's best that I consult with those who have more experience with magic then voodoo.
I currently handle setting cookies in my PHP class file where I handle the creation of all the PHP classes that I use.
Here is essentially what my class file looks like with a two examples of settings that visitors can manipulate on my site...
Code: Select all
<?php
class settings {
public function set($name,$value) {$this->$name = $value;}
public function get($name){return $this->$name;}
}
if ($_GET['audio'] == "0") {$trueaudio = 0; if (!headers_sent()) {setcookie('audio','0',time()+2592000,'/');}}
else if ($_GET['audio'] == "1") {$trueaudio = 1; if (!headers_sent()) {setcookie('audio','1',time()+2592000,'/');}}
else if ($_GET['audio'] == "2") {$trueaudio = 2; if (!headers_sent()) {setcookie('audio','2',time()+2592000,'/');}}
else if (isset($_GET['audio'])) {$error = audio; $trueaudio = 0;}
else if ($_COOKIE['audio'] == "0") {$trueaudio = 0;}
else if ($_COOKIE['audio'] == "1") {$trueaudio = 1;}
else if ($_COOKIE['audio'] == "2") {$trueaudio = 2;}
else {$trueaudio = 0;}
if ($_GET['connection'] == "1") {$trueconnection = 1; if (!headers_sent()) {setcookie('connection','1',time()+2592000,'/');}}
else if ($_GET['connection'] == "2") {$trueconnection = 2; if (!headers_sent()) {setcookie('connection','2',time()+2592000,'/');}}
else if ($_GET['connection'] == "r") {$trueconnection = r; if (!headers_sent()) {setcookie('connection','r',time()-2592000,'/');}}
else if (isset($_GET['connection'])) {$error = connection; $trueconnection = 0;}
else if ($_COOKIE['connection'] == "1") {$trueconnection = 1; if (!headers_sent()) {setcookie('connection','1',time()+2592000,'/');}}
else if ($_COOKIE['connection'] == "2") {$trueconnection = 2; if (!headers_sent()) {setcookie('connection','2',time()+2592000,'/');}}
else {$trueconnection = 0;}
// Assign subclasses to true values.
$settings = new settings();
$settings->set('audio',$trueaudio);
$settings->set('connection',$trueconnection);
/*
Echo classes...
echo $settings->get('audio');
echo $settings->get('connection');
*/
?>I hope all of this makes sense! So how should I start approaching this?