惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
The GitHub Blog
The GitHub Blog
小众软件
小众软件
美团技术团队
博客园 - 司徒正美
G
Google Developers Blog
Blog — PlanetScale
Blog — PlanetScale
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
大猫的无限游戏
大猫的无限游戏
罗磊的独立博客
Recent Announcements
Recent Announcements
酷 壳 – CoolShell
酷 壳 – CoolShell
D
Docker
J
Java Code Geeks
Last Week in AI
Last Week in AI
V
Visual Studio Blog
Microsoft Azure Blog
Microsoft Azure Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Proofpoint News Feed
V
V2EX
C
Check Point Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
MyScale Blog
MyScale Blog

博客园 - 网魂小兵

Docker for MAC SqlServer将没有log文件的数据库文件附加到服务器中 [mongodb-10gen]ubuntu下安装方法 [GO编程] GO入门语法基础 [GO编程]GO编程环境 IE6 css fixed [node.js]小释 跟我一起学习ASP.NET 4.5 MVC4.0(六) 跟我一起学习ASP.NET 4.5 MVC4.0(五) 跟我一起学习ASP.NET 4.5 MVC4.0(四) 跟我一起学习ASP.NET 4.5 MVC4.0(三) 跟我一起学习ASP.NET 4.5 MVC4.0(二) 跟我一起学习ASP.NET 4.5 MVC4.0(一) VS11在Win8下的安装和体验 Win8安装体验 ASP.NET MVC4.0展望 .NET命名规范和开发约定 安装TFS2010的注意事项 VS2010类模板更改,增加版权等等信息
Cookie标注
网魂小兵 · 2012-09-26 · via 博客园 - 网魂小兵

在实现验证或其他状态保存中,我不喜欢使用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  */

这个Path问题困扰了我好几天,一不小心想明白了这个问题,所以留下笔记记一下!