...

View Full Version : Array in cookie



usmanrazzaq
06-07-2007, 11:16 AM
Hello All
i have a problem in storing arrays in cookie i want to store or make cookie in which i can store multidimentional array........is it posible if yes then how i can do this?

thanks in advance

kbluhm
06-07-2007, 12:16 PM
Have a look at serialize() and unserialize().

Pennimus
06-07-2007, 12:29 PM
I'm also interested in this subject but didn't find the manual particularly eluminating on this occasion.

Lets say I have a site with 5 pages, on each one users can vote on the page in order to say whether it's good or bad.

This is an anonymous function so I can't use a database to store whether users have voted on each page or not, but I only want to allow each individual one vote (obviously cookies aren't full proof, but they're better than nothing).

1) Is there some tutorial out there that would give me a practical example of how to use serialize() to store everything in one cookie, rather than dropping a cookie on every page?

2) What if there were 10 pages. 20? 50? 1000? At what point does it become ridiculous trying to store all this in one cookie?

firepages
06-07-2007, 01:47 PM
better off using sessions but the basics are the same


<?php
$data=array(
array('blah'=>'blah'),
array('blah'=>'blah')
);
setcookie('data',serialize($data));
//..................another page
print_r(unserialize($_COOKIE['data']));
?>

digital-ether
06-07-2007, 03:24 PM
This is an anonymous function so I can't use a database to store whether users have voted on each page or not, but I only want to allow each individual one vote (obviously cookies aren't full proof, but they're better than nothing).

If you can store it in a cookie, why can't you store it in a database? Are you setting cookies client side? Even then you can still store it in a database.



2) What if there were 10 pages. 20? 50? 1000? At what point does it become ridiculous trying to store all this in one cookie?

The cookies can easily be modified so its better to use sessions, like said.
The cookies are sent in HTTP headers in each HTTP Response, and HTTP Request where you set a cookie. This can be up your bandwidth a lot (even worse for "Web2.0" Sites).
The HTTP Specification recommends a limit on the amount of data that can be saved on the client via cookies.
Lowest being IE6/7 which limits to 20 cookies per domain and total of 4KB (including temporary cookies such as sessionIDs).
So you have to keep beneath this limit.
Theoretically the best you would get space-wise would be to save in multiple cookies, since saving a serialized array includes the Array keys and information/syntax needed to unserialize the array .

If you saved in a PHP session, however, all you have to worry about is the overhead when PHP reads/writes the session data to the local storage such as disk/db etc.
If you're saving a small amount of data, like a preferred color, cookies are good. But if you already have a PHP session, then you already have the overhead of opening the connection to the local storage, writes/reads/updates are negligible load for the amount of data that usually goes to cookies.

Pennimus
06-07-2007, 07:39 PM
If you can store it in a cookie, why can't you store it in a database?

Based on what identifiable information about the user? It's an anoymous thing, i.e. anyone can do it, you don't have to be registered etc... perhaps I didn't make that clear.

Re: Sessions. Don't those expire?

PS: Thanks for the code Firepages.

digital-ether
06-08-2007, 01:36 AM
Based on what identifiable information about the user? It's an anoymous thing, i.e. anyone can do it, you don't have to be registered etc... perhaps I didn't make that clear.

Re: Sessions. Don't those expire?

PS: Thanks for the code Firepages.

Yes, the browser dumps temporary session cookies as soon as it is closed. (cookies that don't specify a expire date)

I guess I was using the wrong term, "sessionID".
What I meant instead of "sessionID" is saving an ID in a persistent cookie.
(Just like PHP's built in sessionID, but with an expire date to make persistent).

eg: based on what was posted already...


<?php
$data=array(
array('blah'=>'blah'),
array('blah'=>'blah')
);
$salt = 'some really long secret string ....';
$browserID = sha1(rand(0, 100000).time().$salt); // your persistent sessionID
setcookie('browserid', $browserID, time()+3600, '/'); // set the persistent cookie for 1 hour.

// now insert the data into your db
$database->query("INSERT INTO table set uid = '".$browserID."', data = '".serialize($data)."'";


//..................another page

// get data from db using id in cookie
$data = $database->query("SELECT data FROM table WHERE uid = '".$_COOKIE['browserid']."' LIMIT 1");
print_r(unserialize($data);
?>

this thus avoids the oversized http headers created if you have the cookies hold the data... and all other cookie problems...

Pennimus
06-08-2007, 10:02 AM
Ahhh, cunning.... me likey :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum