Set-Cookie:过期属性、时钟偏差和 Internet Explorer 问题 [英] Set-Cookie: Expire property, clock skew and Internet Explorer issue

查看:56
本文介绍了Set-Cookie:过期属性、时钟偏差和 Internet Explorer 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个头部 Max-Age 允许指定 cookie 的过期时间.不幸的是,Internet Explorer 6、7、8 以及可能更高版本不支持 Max-Age,并且需要具有 GMT 绝对日期的 Expires 标头.

There is a header Max-Age that allows to specify the expiration time of a cookie. Unfortunately Internet Explorer 6, 7, 8 and probably later do not support Max-Age and require Expires header with an absolute date in GMT.

特定客户端上的 GMT 时间和 TZ 设置可能不正确的情况并不少见.考虑用户没有正确定义他的时区并手动调整时钟.

It is not uncommon that GMT time and TZ settings on specific client may be incorrect. Consider user that had not defined his time zone correctly and adjusts the clock manually.

更重要的是,有时可能会有很多分钟的明显时钟偏差,而用户没有意识到它们.

More than that, sometimes there may be a significant clock skew of many minutes that the user is unaware of them.

在这种情况下,它的 GMT 时间可能会移动到几个小时.它可以有效地防止服务器设置任何 cookie 需要较短的过期时间.考虑到如果 TZ 不正确,则永远不会设置最长期限为 10 分钟的 cookie.

In such a case its GMT time may be shifted up to several hours. Effectively it would prevent from a server to set any cookie that requires short expiration time. Consider a cookie that has maximal age of 10 minutes would never be set if TZ is incorrect.

关于如何解决问题的原始想法(不起作用或有问题):

  1. 当然最好是使用 Max-Age 或者甚至指定两者,因为所有浏览器都会忽略Expire"部分 - 但它在 IE 中不起作用
  2. 我想到的另一种方法是设置 Date: header 希望 IE 知道计算差异以解决时钟偏差......但这对 IE 没有帮助.
  3. 根据请求从客户端获取时间(使用 JavaScript),然后计算时钟差异,然后根据需要调整 Expire 标头.但是,它需要复杂的数据操作,包括将时间提交给服务器的某种方式.

问题:

  1. 在 IE 中处理 cookie 的过期时间的最佳和常见做法是什么?
  2. 你如何在你的应用程序中做到这一点

推荐答案

  • 将 Max-Age 设置为除 Microsoft 之外的所有人都理解的设置.
  • 添加仅在 IE 上运行的 Javascript,以根据浏览器的时钟将 Max-Age 转换为 UTC,并在 cookie 上设置该过期时间.请注意,JavaScript 无法读取 cookie 中设置的 Max-Age,因此您必须以其他方式向 JavaScript 提供该信息(以及任何其他选项).
  • 来自 QuirksMode

    function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
    }
    

    然后在您从某处获取 cookie namemaxAgeotherOptions(例如路径、域)之后:

    Then after you get the cookie name and maxAge and otherOptions (e.g. path, domain) from somewhere:

    var date = new Date();
    date.setTime(date.getTime() + (maxAge * 1000));
    document.cookie = name + "=" + readCookie(name) + 
        '; expires=' + date.toUTCString() + otherOptions
    

    这篇关于Set-Cookie:过期属性、时钟偏差和 Internet Explorer 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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