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

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

问题描述

有一个标头Max-Age允许指定cookie的过期时间。不幸的是,Internet Explorer 6,7和8以及以后可能不支持Max-Age,并且要求Expires标头的绝对日期在GMT。

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或者甚至指定两者,因为所有浏览器都会忽略过期部分 - 但它不工作在IE

  2. 另一种方式,我想是设置Date:header希望IE将知道计算差异工作时钟偏差...但它不会帮助IE。

  3. 根据请求(使用JavaScript)获取客户端的时间,然后计算时钟差异,然后根据需要调整Expire标头。但是,它需要复杂的数据操作,包括向服务器提交时间的一些方法。

问题: / p>

Questions:


  1. 在IE中处理Cookie的过期时间是最好的还是常见的做法?



推荐答案

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 name maxAge otherOptions (例如路径,域):

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天全站免登陆