Page 1 of 1

storing form preferences is cookies

Posted: Wed Feb 22, 2006 8:20 am
by s.dot
I've used cookies before, but I only use them to store one value.

However, I want to store a users search preferences so they don't have to fill out the form every time they visit the page. There are about 10 forms and I really don't want to store it in 10 separate cookies (i don't think there's a need to, right?)

So how could/should I go about doing this? Obviously I need to retain the field name along with the value chosen. That could get a little messy storing in plain text.

Should I choose a unique delimiter such as # and explode("#") when pulling the information back from the cookie?

What about storing it into an array and serializing it, then unserializing it upon pulling it back out?

What would the best way to go about this be.. or is it up to me?

Posted: Wed Feb 22, 2006 9:40 am
by feyd
Not only is there no need to, you often can't. Browsers that support cookies are only recommended to allow 4 cookies per site.

Serializing in some fashion would likely be the best way. Gambler posted, recently, an alternate to serialize() which appears to save space quite nicely. I haven't tested it, but you may find it useful.
viewtopic.php?t=44248

Posted: Wed Feb 22, 2006 10:06 am
by s.dot
Hmm, serializing the array won't be TOO long.

There are 8 (i counted) preferences. And I coded it so the possible values are all integers. The max size of each integer will be 7 of the form fields will be 2 digits. 1 form field will contain 5 or 6 digits.

What is the max amount of info a cookie is allowed to hold?

Posted: Wed Feb 22, 2006 10:17 am
by feyd
4K of data is the maximum a browser is supposed to allow, but if you get above 1K, you're doing something wrong. :P