Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One cookie with many values or many cookies with one value?

Tags:

php

cookies

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?

like image 368
ryeguy Avatar asked Sep 07 '09 23:09

ryeguy


People also ask

Can one cookie have multiple values?

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.

What is the maximum amount of data that can be stored in a cookie?

To comply with the standard, you should store no more than 4096 bytes per cookie.


2 Answers

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.

like image 162
chaos Avatar answered Oct 26 '22 23:10

chaos


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.

like image 21
karim79 Avatar answered Oct 26 '22 22:10

karim79