Advanced Cookies multiple properties+values in single cookie

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Advanced Cookies multiple properties+values in single cookie

Post by JAB Creations »

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. :mrgreen:

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. :mrgreen:

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'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?
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Re: Advanced Cookies multiple properties+values in single cookie

Post by Rovas »

First of all do you really need to send all those values to the user?
The values that you put into the cookies are so large that you need separate cookies? Try to trim down the values by using numbers or codes formed from three letters.
Why don' t you try to combine the cookies into a few separate categories. Let' s say: display for cursors, css3 etc; info: connection; chat:chatroom, initialfocus. Also you can use a string or a array to store data.
And of course there is session you could use.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Advanced Cookies multiple properties+values in single cookie

Post by JAB Creations »

Wasted Time Post Reply - This post is not helpful to the thread, please skip ahead.
First of all do you really need to send all those values to the user?
...
The values that you put into the cookies are so large that you need separate cookies?
...
Why don' t you try to combine the cookies...
...

Do every one a favor and don't bother replying to anyone's posts unless you actually bother to read them.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Advanced Cookies multiple properties+values in single cookie

Post by VladSun »

I think, you need to construct an array, fill it with your values and serialize it in the cookie.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Advanced Cookies multiple properties+values in single cookie

Post by JAB Creations »

Ok so my first question would be what would I use to replace the setcookie methods in my class file's procedural programming? I'll obviously create the array afterwards, so I'll need to somehow pass that information to the array. I could also just stick to $true variables (in example $trueaudio and $trueconnection). I'm not sure how to construct the array and then deconstruct it later on for reading...which is another issue...am I going to need two separate scripts to construct after the procedural programming and one to deconstruct it before the procedural programming? I would presume yes?
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Advanced Cookies multiple properties+values in single cookie

Post by JAB Creations »

Now this looks exactly like something I'll be interested in using!

Code: Select all

<?php
$pizza  = "audio=1,backgroundimages=1,chatroom=0,connection=2,counter=43,css3=1,cursors=0,dhtmleffects=1,initialfocus=search";
$pieces = explode(",", $pizza);
echo $pieces[0].'<br>'; // piece1
echo $pieces[1].'<br>'; // piece2
echo $pieces[2].'<br>'; // piece2
echo $pieces[3].'<br>'; // piece2
echo $pieces[4].'<br>'; // piece2
echo $pieces[5].'<br>'; // piece2
echo $pieces[6].'<br>'; // piece2
?>
...thoughts on explode function?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Advanced Cookies multiple properties+values in single cookie

Post by VladSun »

My first post-answer will be some coding critique - creative of course, no hard feelings.
Instead of:

Code: Select all

 
      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;}
 
isn't it better to use:

Code: Select all

 
$audio_types = array (
    "0" => 0,
    "1" => 1,
    "2" => 2,
);
 
$true_audio =   isset($_GET['audio']) && isset($audio_types[$_GET['audio']]) 
                ? 
                $audio_types[$_GET['audio']]
                : 
                (
                    isset($_COOKIE['audio']) && isset($audio_types[$_COOKIE['audio']]) 
                    ?
                    $audio_types[$_COOKIE['audio']]
                    :
                    0
                );
if (!headers_sent()) 
              setcookie('audio', $true_audio, time() + 2592000, '/');
 
Some thoughts:
1. Use "elseif" instead of "else if" (C/C++ programmer ;) )
2. I think it would be better to increase the "cookie life time" every time the user visits your site.
3. I would write my code "$true_audio = ..." on single line ;)
4. Using array of possible values simplifies code modifications due to new requirements.

So, wait for my second post :)
Last edited by VladSun on Thu Apr 24, 2008 5:07 pm, edited 2 times in total.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Advanced Cookies multiple properties+values in single cookie

Post by VladSun »

And if you serialize your array in a cookie you will have write function:

Code: Select all

 
$settings['audio'] = $audio;
.....
setcookie('settings', serialize($settings), time() + 2592000, '/');
 
and read function:

Code: Select all

 
$_COOKIE['settings'] = unserialize($_COOKIE['settings']);
....
$audio = $_COOKIE['settings']['audio'];
 
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Advanced Cookies multiple properties+values in single cookie

Post by JAB Creations »

I'm always open to improvements on my existing code, just not being told that I should not strive to achieve my goals.

PHP has seriously numbed the pain of doing things and I have to admit this is one of them...

Code: Select all

<?php
$pizza  = "audio=1,backgroundimages=1,chatroom=0,connection=2,counter=43,css3=1,cursors=0,dhtmleffects=1,initialfocus=search";
$pieces = explode(",", $pizza);
echo $pieces[0].'<br>'; // piece1
echo $pieces[1].'<br>'; // piece2
echo $pieces[2].'<br>'; // piece2
echo $pieces[3].'<br>'; // piece2
echo $pieces[4].'<br>'; // piece2
echo $pieces[5].'<br>'; // piece2
echo $pieces[6].'<br>'; // piece2
 
$array = array($pieces[4], $pieces[5], $pieces[6]);
$comma_separated = implode(",", $array);
 
echo $comma_separated; // lastname,email,phone
?>
I am making the connections between your points for the most part...

My cookies are only set if there is a $_GET request for them...so some of the cookies would expire after a month.

With the explode/implode functions the settings cookie would almost literally be impossible to expire since $_GETs are all over my website. I'll take a closer look at your suggested code, it looks promising! :mrgreen: I just need to down a Red Bull to help wake my mind up is all.

I think I need a clearer explanation of serialize, it looks cool but this is one example of where php.net starts to sound a little alien to me. Thanks!
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Advanced Cookies multiple properties+values in single cookie

Post by JAB Creations »

On line 11 what does the colon : function as?

On line 14 what does the question mark ? function as?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Advanced Cookies multiple properties+values in single cookie

Post by VladSun »

Code: Select all

 
$a = $b ? $c : $d;
 
===

Code: Select all

 
if ($b) $a = $c;
else $a = $d;
 
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Advanced Cookies multiple properties+values in single cookie

Post by JAB Creations »

I mean . is to concatenation as ? is to...?

...and . is to concatenation as : is to...?

Examples are great but they have to be repetitive for me to detect the pattern...

1x1=1
1x2=2
1x3=3
1x4=4

2x1=2
2x2=4
2x3=6
2x4=8

Simple repetition is how I learn best which doesn't really say much for how my brain initially interacts with moderately complex patterns. :|

Thanks!
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Advanced Cookies multiple properties+values in single cookie

Post by VladSun »

JAB Creations wrote:I mean . is to concatenation as ? is to...?

...and . is to concatenation as : is to...?
~

? == if
: == else

or

Code: Select all

 
$a = $b ? $c : $d;
 
===

Code: Select all

 
function ?($b)
{
  if ($b) return $c;
  else return $d;
}
 
 
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Advanced Cookies multiple properties+values in single cookie

Post by JAB Creations »

That makes it difficult to reference, what is the point of replacing a referenceable name (in example php.net/if and php.net/else) with a symbol that is difficult to reference? I see no mention of ? to if on php.net/if and no mention of : to else on php.net/else. Where can I find these referenced please?

Ok so adding repetition I'm trying to grasp your example...

Code: Select all

if ($x) {$a = $c;}
else if ($y) {$a = $d;}
else if ($z) {$a = $e;}
So...

Code: Select all

$a = $b ? $c : $d;
I read that as $a equals $b if $c else $d... (mixing JavaScript detection)

Which in to more refined English to me means $a equals $b if $c or $d exists; which doesn't exactly fit the other example. So I'm probably reading it wrong or...well I'm not sure. Missing parenthesis and curly brackets further confuses me. :mrgreen:
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Advanced Cookies multiple properties+values in single cookie

Post by VladSun »

http://us3.php.net/manual/en/language.o ... arison.php
Ternary Operator
The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE.
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply