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?
A cookie can contain only one “string” value in JavaScript, and storing multiple key-value pairs requires multiple cookies. However, using multiple cookies can also cause some problems.
To comply with the standard, you should store no more than 4096 bytes per cookie.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With