开发者

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

开发者 https://www.devze.com 2023-01-23 04:45 出处:网络
Is it possible to serialize Javascript object variable and store into 开发者_如何学运维cookies? Or is there other way to accomplish the same thing?If these objects aren\'t sensitive (I.e., you don\'t

Is it possible to serialize Javascript object variable and store into 开发者_如何学运维cookies? Or is there other way to accomplish the same thing?


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!


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?


Yes, it's possible, if the resulting string does'nt exceed the limit of the cookie-size(4KB)

0

精彩评论

暂无评论...
验证码 换一张
取 消