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,如需转载请自行联系原作者

相关文章
|
2月前
|
安全 数据库
17、cookie注入
17、cookie注入
40 0
|
11月前
|
存储 Java
cookie 生命周期和cookie有效路径超级详细讲解
cookie 生命周期和cookie有效路径超级详细讲解
118 0
|
Java
会话技术概述、Cookie常用属性和方法及Session常用方法和生命周期
会话技术概述、Cookie常用属性和方法及Session常用方法和生命周期
153 0
|
存储 JSON 前端开发
接口测试(28)彻底理解cookie,session,token的使用及原理2
我们也不一定需要等到token自动失效,token有撤回的操作,通过token revocataion可以使一个特定的token或是一组有相同认证的token无效。如果我们将已验证的用户的信息保存在Session中,则每次请求都需要用户向已验证的服务器发送验证信息(称为Session亲和性)。我们甚至能基于创建一个基于权限的token传给第三方应用程序,这些第三方程序能够获取到我们的数据(当然只有在我们允许的特定的token)在这之前,程序都是通过在服务端存储的登录信息来辨别请求的。尤其是在可扩展性方面。
接口测试(28)彻底理解cookie,session,token的使用及原理2
|
存储 负载均衡 算法
接口测试(27)session、cookie和token的区别1
2、但是随着交互式Web应用的兴起,像在线购物网站,需要登录的网站等等,马上就面临一个问题,那就是要管理会话,必须记住哪些人登录系统, 哪些人往自己的购物车中放商品, 也就是说我必须把每个人区分开,这就是一个不小的挑战,因为HTTP请求是无状态的,所以想出的办法就是给大家发一个会话标识(session id), 说白了就是一个随机的字串,每个人收到的都不一样, 每次大家向我发起HTTP请求的时候,把这个字符串给一并捎过来, 这样我就能区分开谁是谁了。如果访问服务器多了, 就得由成千上万,甚至几十万个。
接口测试(27)session、cookie和token的区别1
|
存储 编解码 应用服务中间件
【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原始真解(下)
springMvc19-RequestHeader(获取请求头中某一部分值
springMvc19-RequestHeader(获取请求头中某一部分值
83 0
springMvc19-RequestHeader(获取请求头中某一部分值
|
存储 Web App开发
Cookie对象的实际应用
Cookie对象的特点和实际应用
|
Web App开发 安全 前端开发
预测最近面试会考 Cookie 的 SameSite 属性
本文就给大家介绍一下浏览器的 Cookie 以及这个"火热"的 SameSite 属性。
141 0
预测最近面试会考 Cookie 的 SameSite 属性
Springboot使用Cookie,生成cookie,获取cookie信息(注解与非注解方式)
Springboot使用Cookie,生成cookie,获取cookie信息(注解与非注解方式)
930 0