• Cookie标注


    在实现验证或其他状态保存中,我不喜欢使用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问题困扰了我好几天,一不小心想明白了这个问题,所以留下笔记记一下!

  • 相关阅读:
    [HNOI2006]超级英雄
    [CTSC1999]家园
    火星探险问题
    [HNOI2008]GT考试
    [USACO14DEC]后卫马克Guard Mark
    [NOI2018]归程
    [USACO15DEC]最大流Max Flow
    [NOIPlus]斗地主
    [LUOGU] P3128 [USACO15DEC]最大流Max Flow
    [BZOJ] 1878: [SDOI2009]HH的项链
  • 原文地址:https://www.cnblogs.com/xdotnet/p/cookie.html
Copyright © 2020-2023  润新知