简体   繁体   English

处理Cookie和数组的最佳方法是什么?

[英]Best way to handle Cookies and arrays?

I want to save basket of liked properties for visitors. 我想为访客保存一篮子喜欢的房产。 The information I need to save is ID, name, URL, and possiby image URL. 我需要保存的信息是ID,名称,URL和可能的图像URL。

What is the best way to do that with cookies? 使用cookie的最佳方法是什么? I dont want to have any login/registration screens. 我不想要任何登录/注册屏幕。 I just want give visitor opportunity to click save to basket and put this into cookies for later visits. 我只想让访问者有机会点击保存到购物篮并将其放入cookie中供以后访问。

Is client side cookie enough for this? 客户端cookie是否足够用于此?

I think that the best thing to do is storing a list of productId within the cookie. 我认为最好的办法是在cookie中存储productId列表。 You are in charge to find the name, URL and image from the ID when you display the basket. 是负责从ID查找名称,URL和图像当显示篮筐。 Not the cookie. 不是cookie。 (Imagine you change the image directory?) Also one cookie should be enough. (想象一下你改变了图像目录?)另外一个cookie就足够了。 Do not make one by product! 不要制作一个副产品!

You don't have to use login screens with cookie, it's basically just a storage. 您不必使用带有cookie的登录屏幕,它基本上只是一个存储。 Did you looked at MSDN on HttpCookies for C# ? 您是否HttpCookies查看了MSDN上的HttpCookies

The drawback of cookies are: cookies的缺点是:

  1. The client may not accept the cookie 客户可能不接受cookie
  2. The client may clear the cookie 客户可以清除cookie

But I think this is not problematic for basket management. 但我认为这对篮子管理来说不成问题。

If your information is small - putting it into cookies is fine (like list of product IDs). 如果您的信息很小 - 将其放入cookie中就可以了(如产品ID列表)。 If you plan to put significant amount of data (about 3K+ into single cookie or 20K+ split between cookies) you should consider other ways to store information - ASP.Net SessionState is one possible option. 如果您计划放入大量数据(大约3K +到单个cookie或20K +在cookie之间拆分),您应该考虑其他方式来存储信息 - ASP.Net SessionState是一种可能的选择。

There are restrictions on length of individual cookie, total cookies size for given domain and total cookie size. 单个cookie的长度,给定域的总cookie大小和总cookie大小都有限制。 Browsers are free to drop some or all cookies when this limits are hit. 当达到此限制时,浏览器可以自由删除部分或全部cookie。 To work around restriction on single cookie lenght you can split value between several cookies. 要解决对单个cookie长度的限制,您可以在几个cookie之间拆分值。

Depending on data you plan to store in the cookie you may need to %-encode values or Base64 encode (for binary data). 根据您计划存储在cookie中的数据,您可能需要% - 编码值或Base64编码(对于二进制数据)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM