Chrome不允许Cookie的到期时间少于4小时 [英] Chrome does not allow cookies with expirations less than 4 hours

查看:189
本文介绍了Chrome不允许Cookie的到期时间少于4小时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在Google Chrome中设置使用寿命较短的Cookie。它们要么没有设置,要么立即被删除(无法知道哪个,尽管结果是相同的)。只有未来到期时间为4小时或更短时,才会发生这种情况。如果到期时间大于4小时,并且在Firefox或Safari中不会出现问题,则相同的代码可以正常工作。以下是一个示例:

I can't seem to set cookies with short lifespans in Google Chrome. They're either not getting set, or are immediately being deleted (can't tell which, although the result is the same either way). This only occurs if the expiration is 4 hours or less in the future. Identical code works fine if the expiration time is greater than 4 hours and the issue does not occur in Firefox or Safari. Here's an example:

无效:

exp = new Date();
exp.setMinutes(exp.getMinutes() + 240);
document.cookie="name=value;expires=" + exp + ";path=/";

工程:

exp = new Date();
exp.setMinutes(exp.getMinutes() + 241);
document.cookie="name=value;expires=" + exp + ";path=/";

有人对如何解决这个问题有任何建议吗?

Does anyone have any suggestions for how to resolve this?

推荐答案

确实我在这里查看了Chromium Source http://code.google.com/p/chromium/source/search?q=document.cookie+expire& origq = document.cookie + expire& btnG = Search + Trunk 引用cookies,并在所有的expires =语句中找到它们在日期对象上调用.toGMTString()或.toUTCString(),因此它可能是p

Indeed I checked out the Chromium Source here http://code.google.com/p/chromium/source/search?q=document.cookie+expire&origq=document.cookie+expire&btnG=Search+Trunk with reference to cookies and found in all their expires= statements they call either .toGMTString() or .toUTCString() on a date object so it might be a peculiar Date Formatting function screwing up when it imlpicitly converts it to a format rather than explictly setting one.?!

而不是这样:

document.cookie="name=value;expires=" + exp + ";path=/";

请尝试:

document.cookie="name=value;expires=" + exp.toUTCString() + ";path=/";

这篇关于Chrome不允许Cookie的到期时间少于4小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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