tags:

views:

421

answers:

4

I guess the title says it all. If I have many settings that I want to store in a cookie, should I create multiple cookies with one option each, or one big cookie with multiple options in a serialized array or something?

Are there any pros/cons for either approach? What do most people do?

+12  A: 

Well, mostly we do sessions -- send a single cookie with an identifier for the user, and store all the option values on the server. But if I really didn't want to do a session for some reason, I suppose I'd probably do the single cookie on account of it creating less network traffic if done properly.

chaos
+1. Putting data on a user's machine = doom.
Jed Smith
@Jed Smith - true for most things, but trivial ones? (e.g. theme=roses)
karim79
+2  A: 

I'd prefer a serialized array.... Cookies are files stored on the client's machine or the server, so less is better.

janoliver
+2  A: 

A good reason to have separate cookies is so each one is independent of the others, that is to say, an individual cookie can then be expired without affecting other ones, which is not possible if you have everything stored in one big cookie.

karim79
+4  A: 

Your options are:

PHP Sessions - No cookies required. Store all the data you want. Once the browser is closed or the session is closed new authentication is needed.

Persistent Sessions - One cookie with one value which is either the session key used to access stored sessions or used to access a database table with session information. You can store all the data you want and you have the benefit of not worrying about signing in all the time.

Cookie Only - Storing multiple values in a cookie is not always the best of ideas. Why? 1) Cookies are insecure and can be read by anyone. 2) keeping up with multiple cookies throughout your application can be a source of future bugs. 3) Depending on the client/browser to provide you with accurate data is never good. If you use cookies, make sure they have as light of a footprint as possible.

Big cookies only give you a tummy-ache. Eat Cake instead.

Dooltaz