0

I need to save a form state (values for input elements).

But the form is not a standard html form so the browser would not save the form state.

I need to store the form state as cookie I cannot save anything on server side.

But the size of the cookie will be around 10 KB. How to do this? Anyone have a idea?

Acn
  • 1,010
  • 2
  • 11
  • 22

1 Answers1

1

Split the 10KB of data into multiple cookies (i.e. cookies with different names in the same domain) when saving and combine those cookie values when retrieving.

If I recall correctly, the standard size of a cookie in most modern browsers is ~4096 bytes (sometimes including the name, sometimes not) so bear this in mind when creating the cookies.

Also bear in mind that the data in the cookies could be tampered with so exercise due diligence when working with such data.

Russ Cam
  • 124,184
  • 33
  • 204
  • 266
  • how can you split the cookie ? or have multiple cookies on same domain ? – c69 Jan 16 '12 at 12:59
  • Rule of thumb is to limit cookie *content* size to 4000 bytes. – bezmax Jan 16 '12 at 13:00
  • @c69 cookies have names. So you can create several cookies with different names in same domain. – bezmax Jan 16 '12 at 13:00
  • @c69 come up with a naming convention for cookies e.g. `my_cookie_{num}` then set the value on `my_cookie_0` first. Once you've reached what you consider a suitable data size (~4000 bytes), then set the value of `my_cookie_1`, etc, etc – Russ Cam Jan 16 '12 at 13:02
  • hmm. true. i always thought 4KB limit is **total** per domain, but even in IE its up to 20 cookies, up to 4KB each. Yet, i think `localStorage` is better suited for the task. Maybe together with fallback solutions - http://stackoverflow.com/questions/4692245/html5-local-storage-fallback-solutions – c69 Jan 16 '12 at 13:15