storing form preferences is cookies

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

storing form preferences is cookies

Post 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?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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
Post Reply