如何创建持久性和非持久性 cookie? [英] How do I create a persistent vs a non-persistent cookie?

查看:66
本文介绍了如何创建持久性和非持久性 cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法弄清楚如何创建持久性和非持久性 cookie.例如,它们在发回的 HTTP 标头方面有何不同?

I can't seem to figure out how to create a persistent vs a non-persistent cookie. How do they differ, say, in the HTTP headers that are sent back?

推荐答案

Cookie 具有隐式或显式设置的过期日期,用于控制它们的持续时间(取决于用户代理实际执行它).Cookie 可能仅在会话期间(或什至更短的时间)持续存在.

Cookies have an expiration date implicitly or explicitly set which controls how long they last (subject to the user agent actually enforcing it). A cookie may persist only for the duration of the session (or an even shorter period).

如果 cookie 有效,它将与 HTTP 请求一起传递到它源自的域.只有设置 cookie 的域才能读取 cookie(尽管有一些方法可以利用它,例如跨站点脚本).

If a cookie is valid, it will be passed along with the HTTP request to the domain that it originated from. Only the domain that set the cookie can read the cookie (though there are ways to exploit this, such as cross-site scripting).

  • 如果您希望 cookie 在特定时间过期,请设置一个过期时间使用您选择的客户端或服务器端语言在其上注明日期.

  • If you want a cookie to expire at a specific time, set an expiration date on it using the client or server-side language of your choice.

如果您希望 cookie 在会话结束时过期,请不要设置到期日期.

If you want the cookie to expire when the session ends, don't set an expiration date.

来自 RFC(重点是我的):

cookie setter 可以指定一个删除日期,在这种情况下cookie 将在该日期删除.

The cookie setter can specify a deletion date, in which case the cookie will be removed on that date.

如果 cookie setter 没有指定一个日期,cookie 将被删除一次用户退出浏览器.

因此,指定日期是一种方式让饼干存活下来会议.为此,cookies具有到期日期的称为坚持不懈.

As a result, specifying a date is a way for making a cookie survive across sessions. For this reason, cookies with an expiration date are called persistent.

作为示例应用程序,一个购物网站可以使用持久化用于存储用户拥有的项目的 cookie放在他们的篮子里.(事实上​​,cookie 可能指的是一个条目存储在购物网站的数据库,不在您的计算机上.)这样,如果用户退出浏览器购买并稍后退货,他们仍然在篮子所以他们不必寻找又是这些项目.如果这些饼干没有给出有效期,它们会在浏览器关闭时过期已关闭,有关的信息购物车内容将丢失.

As an example application, a shopping site can use persistent cookies to store the items users have placed in their basket. (In reality, the cookie may refer to an entry in a database stored at the shopping site, not on your computer.) This way, if users quit their browser without making a purchase and return later, they still find the same items in the basket so they do not have to look for these items again. If these cookies were not given an expiration date, they would expire when the browser is closed, and the information about the basket content would be lost.

这篇关于如何创建持久性和非持久性 cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆