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

前端 未结 4 400
慢半拍i
慢半拍i 2021-02-07 06:43

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

相关标签:
4条回答
  • 2021-02-07 07:08

    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.

    0 讨论(0)
  • 2021-02-07 07:20

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

    0 讨论(0)
  • 2021-02-07 07:23

    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.

    0 讨论(0)
  • 2021-02-07 07:27

    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.

    0 讨论(0)
提交回复
热议问题