Cookie标注

本文涉及的产品
.cn 域名,1个 12个月
简介:

在实现验证或其他状态保存中,我不喜欢使用Session,因为在我认为经常会丢失数据,而且SessionID也是基于Cookie来保存的。最近几天我做了个多语言的小软件,使用Cookie来保存当前用户选择的Cookie,使用jQuery.Cookie插件来设置,对于这个Cookie得path搞得一塌糊涂。在这里记录一下我对此的理解,问题也能够得到相应的解决。

1.Path

在.NET中Cookie的的保存获取就不需要多说了,百度一下基本很多,而且都可以使用,就是这个Path以前一直没有注意的问题。这个Path描述为虚拟目录,Cookie允许不同目录下的值各自独立。如果没有指定这个Path,默认就是当前虚拟路径,这样就会造成了,设置语言的时候,不同路径下的语言没能够同步解决。其实解决的问题很简单,将Path设置为根目录即可,就是“/”。当然如果在当前目录下设置同样的键的Cookie,将覆盖根目录的Cookie,因为当前目录下的Cookie比较优先。

2.Domain

如果想解决子域名同样适用这个Cookie,那就可以将Domain设置为域名地址,那样相同域名下的二级,三级等域名将共享这个Cookie,当然上面提到的Path也需要设置相同的目录,否则是得不到的。在验证用户保存状态中一般设置为个目录即“/”,这个目录是整站公用的目录,所以在相同站点下的所有目录的这个Cookie值都是可以得到的。

3.Expires

过期时间,这个主要设置过期时间的长短,在jQuery中可以有两种设置方法:如果是数字则表示天数,从建立Cookie算起;如果是时间则表示特定的时间。在.NET中就是时间实例对象,如果需要移除这个Cookie可以将这个时间设置为过去的某一个时间,那样Cookie就会消失。

4.Secure

是否使用安全SSL进行访问,即HTTPS。

 

以下是jQuery.Cookie的使用文档,基本很简单顺便贴上:

复制代码
 1 /**
 2  * Create a cookie with the given name and value and other optional parameters.
 3  *
 4  * @example $.cookie('the_cookie', 'the_value');
 5  * @desc Set the value of a cookie.
 6  * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
 7  * @desc Create a cookie with all available options.
 8  * @example $.cookie('the_cookie', 'the_value');
 9  * @desc Create a session cookie.
10  * @example $.cookie('the_cookie', null);
11  * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
12  *       used when the cookie was set.
13  *
14  * @param String name The name of the cookie.
15  * @param String value The value of the cookie.
16  * @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
17  * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
18  *                             If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
19  *                             If set to null or omitted, the cookie will be a session cookie and will not be retained
20  *                             when the the browser exits.
21  * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
22  * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
23  * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
24  *                        require a secure protocol (like HTTPS).
25  * @type undefined
26  *
27  * @name $.cookie
28  * @cat Plugins/Cookie
29  * @author Klaus Hartl/klaus.hartl@stilbuero.de
30  */
31 
32 /**
33  * Get the value of a cookie with the given name.
34  *
35  * @example $.cookie('the_cookie');
36  * @desc Get the value of a cookie.
37  *
38  * @param String name The name of the cookie.
39  * @return The value of the cookie.
40  * @type String
41  *
42  * @name $.cookie
43  * @cat Plugins/Cookie
44  * @author Klaus Hartl/klaus.hartl@stilbuero.de
45  */
复制代码

本文转自网魂小兵博客园博客,原文链接:http://www.cnblogs.com/xdotnet/archive/2012/09/26/cookie.html,如需转载请自行联系原作者

相关文章
|
4月前
|
安全 数据库
17、cookie注入
17、cookie注入
43 0
|
4月前
|
存储 搜索推荐 安全
【Cookie和Session辨析】
【Cookie和Session辨析】
26 2
|
4月前
|
安全 Java
解决Cookie值不允许出现中文的问题
通过将中文字符串编码为URL安全格式,并在需要时进行解码,你可以解决Cookie值不允许出现中文的问题。这样可以确保Cookie值的正确性和完整性。 买CN2云服务器,免备案服务器,高防服务器,就选蓝易云。百度搜索:蓝易云
20 0
|
存储 Java
cookie 生命周期和cookie有效路径超级详细讲解
cookie 生命周期和cookie有效路径超级详细讲解
135 0
|
Java
会话技术概述、Cookie常用属性和方法及Session常用方法和生命周期
会话技术概述、Cookie常用属性和方法及Session常用方法和生命周期
161 0
|
NoSQL Redis
SpringSession的源码解析(从Cookie中读取Sessionid,根据sessionid查询信息全流程分析)
上一篇我们介绍了SpringSession中Session的保存过程,今天我们接着来看看Session的读取过程。相对保存过程,读取过程相对比较简单。 本文想从源码的角度,详细介绍一下Session的读取过程。
324 0
SpringSession的源码解析(从Cookie中读取Sessionid,根据sessionid查询信息全流程分析)
|
存储 应用服务中间件 数据安全/隐私保护
【JavaWeb】会话跟踪技术Cookie与Session原始真解(上)
文章目录 1 什么是会话? 2 Cookie技术 2.1 Cookie简介 2.2 Cookie的理解与创建 2.3 服务器获取Cookie与Cookie的修改 2.4 Cookie的生命控制与生命周期 2.5 Cookie有效路径Path设置 3 Session会话技术 3.1 初探Session 3.2 Session的创建、获取与基本使用 3.3 Session的生命控制与生命周期 3.4 如何理解Session底层是基于Cookie实现的?
【JavaWeb】会话跟踪技术Cookie与Session原始真解(上)
|
存储 编解码 应用服务中间件
【JavaWeb】会话跟踪技术Cookie与Session原始真解(下)
文章目录 1 什么是会话? 2 Cookie技术 2.1 Cookie简介 2.2 Cookie的理解与创建 2.3 服务器获取Cookie与Cookie的修改 2.4 Cookie的生命控制与生命周期 2.5 Cookie有效路径Path设置 3 Session会话技术 3.1 初探Session 3.2 Session的创建、获取与基本使用 3.3 Session的生命控制与生命周期 3.4 如何理解Session底层是基于Cookie实现的?
【JavaWeb】会话跟踪技术Cookie与Session原始真解(下)
|
存储 Web App开发
Cookie对象的实际应用
Cookie对象的特点和实际应用
|
Web App开发 安全 前端开发
预测最近面试会考 Cookie 的 SameSite 属性
本文就给大家介绍一下浏览器的 Cookie 以及这个"火热"的 SameSite 属性。
148 0
预测最近面试会考 Cookie 的 SameSite 属性