Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to serialize Javascript object variable and store into cookies?

Tags:

javascript

Is it possible to serialize Javascript object variable and store into cookies? Or is there other way to accomplish the same thing?

like image 279
Mark K Avatar asked Nov 04 '10 01:11

Mark K


2 Answers

If these objects aren't sensitive (I.e., you don't care if your users modify them), then serializing them into cookies is fine, provided that your objects are small enough not to cause issue.

If your cookies ARE sensitive (you need to depend on them to a level of integrity) or you have large structures, then why not consider storing these serialized objects in a persistant session that is stored on your server. You can then use the cookies as a key or ID to know which session to restore when your visitor returns. In this manner, the size of your serialized objects and whether they might 'fit' in a cookie is no longer relevant.

Another possibility if you not fussy about users modifying things, but do require ample space, (although may not work for all browsers,) is to create a HTML5 'local database' or client-side storage. In this manner, you are both eliminating your concern about the size of the cookies as well as the growing size of your own server-side database. This is probably the best option for sites where you want to store a lot of data per user, but you're not sure if they'll ever come back again. You can always resort to server-side storage (see above) for older browsers.

Here's a particularly good tutorial for getting started with HTML5 local databases: http://blog.darkcrimson.com/2010/05/local-databases/

I hope this is helpful & good luck!

like image 193
mkoistinen Avatar answered Sep 30 '22 17:09

mkoistinen


I don't see why not if it fit into length limit of the cookie. I would convert serialized object into say Base64 though.

What problem you're solving?

like image 20
maximdim Avatar answered Sep 30 '22 15:09

maximdim