Cookie 设置两次;如何删除重复项? [英] Cookie is set twice; how to remove the duplicate?

查看:43
本文介绍了Cookie 设置两次;如何删除重复项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个网站,它使用 cookie 来记住每次访问时的当前布局状态.一切都很好,直到我在网站上添加了一个 Facebook 的喜欢"按钮,该按钮会生成允许用户共享特定 UI 状态的链接(有点令人困惑,但与问题并不真正相关).

So I have a website that uses a cookie to remember the current layout state across visits. Everything was working great until I added a Facebook 'Like' button to the site which generates links that allow users to share a certain UI state (a little confusing but not really relevant to the problem).

问题是,当我通过这些 Facebook 链接之一访问该站点时,似乎创建了我的布局 cookie 的第二个副本(例如,我看到两个具有相同名称和不同值的 cookie).这不会太糟糕,只是重复 cookie 的值似乎被卡住了,再加上当用户返回站点时,浏览器记住卡住的值而不是最近设置的值(所以它有点像就像有一个我仍然可以使用的好"cookie 和一个我不能使用的坏"cookie,并且浏览器喜欢记住坏"cookie 而不是好"cookie).这破坏了我的布局跟踪/记忆功能.

The problem is that when I visit the site via one of these Facebook links a second copy of my layout cookie seems to be created (as in, I see two cookies with the same name and different values). This wouldn't be too terrible except that the value of the duplicate cookie appears to be stuck, coupled with the fact that when the user returns to the site the browser remembers the stuck value instead of the most recently set value (so it's kind of like there's a "good" cookie that I can still work with, and a "bad" one which I cannot, and the browser likes to remember the "bad" cookie instead of the "good" cookie). This breaks my layout tracking/remembering functionality.

所以这里有两个问题:

  1. 我如何阻止这种情况发生/首先为什么会发生这种情况?
  2. 如何为已经卡住 cookie 的用户解决问题(我知道我可以为 cookie 选择一个新名称,但我更愿意找到一种方法来正确解开卡住的 cookie)?

如果我在访问页面卡住后使用 Chrome 的开发者控制台,我可以看到 document.cookie 是(为了可读性添加了格式):

If I use Chrome's developer console after visiting the page in a stuck state, I can see that document.cookie is (formatting added for readability):

layoutState=[{'id':6,'x':8,'y':1525,'z':4,'url':'undefined'}, {'id':1,'x':625,'y':709,'z':2,'url':'undefined'}, {'id':2,'x':8,'y':37,'z':3,'url':'undefined'}, {'id':3,'x':625,'y':1179,'z':5,'url':'undefined'}, {'id':4,'x':626,'y':37,'z':1,'url':'undefined'}, {'id':5,'x':626,'y':357,'z':1000000,'url':'http://m.xkcd.com/303/'}]; 
WibiyaNotification1=1; 
WibiyaNotification213286=213286; 
WibiyaNotification213289=213289; wibiya756904_unique_user=1; 
JSESSIONID=DONTHIJACKMEPLEASE; 
WibiyaProfile={"toolbar":{"stat":"Max"},"apps":{"openApps":{}},"connectUserNetworks":[null,null,null,null,null,null]}; 
WibiyaLoads=59; 
layoutState=[{'id':6,'x':8,'y':1525,'z':4,'url':'undefined'}, {'id':1,'x':625,'y':709,'z':2,'url':'undefined'}, {'id':2,'x':8,'y':37,'z':3,'url':'undefined'}, {'id':3,'x':625,'y':1179,'z':5,'url':'undefined'}, {'id':4,'x':626,'y':37,'z':1,'url':'undefined'}, {'id':5,'x':626,'y':357,'z':6,'url':'http://m.xkcd.com/303/'}]"

忽略 Wibiya cookie 和 JSESSIONID.卡住的 cookie 是第一个 'layoutState' 实例,我仍然可以在 JavaScript 中操作的是第二个 'layoutState' 实例.如果我改变一些东西,我会得到以下结果:

Ignore the Wibiya cookies and the JSESSIONID. The stuck cookie is the first 'layoutState' instance, and the one that I can still manipulate in JavaScript is the second 'layoutState' instance. Here is what I get if I change some things around:

layoutState=[{'id':6,'x':8,'y':1525,'z':4,'url':'undefined'}, {'id':1,'x':625,'y':709,'z':2,'url':'undefined'}, {'id':2,'x':8,'y':37,'z':3,'url':'undefined'}, {'id':3,'x':625,'y':1179,'z':5,'url':'undefined'}, {'id':4,'x':626,'y':37,'z':1,'url':'undefined'}, {'id':5,'x':626,'y':357,'z':1000000,'url':'http://m.xkcd.com/303/'}]; 
WibiyaNotification1=1;
WibiyaNotification213286=213286; 
WibiyaNotification213289=213289; 
wibiya756904_unique_user=1; 
JSESSIONID=DONTHIJACKMEPLEASE; 
WibiyaProfile={"toolbar":{"stat":"Max"},"apps":{"openApps":{}},"connectUserNetworks":[null,null,null,null,null,null]}; 
WibiyaLoads=59; 
layoutState=[{'id':1,'x':8,'y':39,'z':1000000,'url':'undefined'}]

第二个layoutState"具有我希望浏览器记住的正确信息.然而浏览器真正记住的是第一个实例的值.

The second 'layoutState' has the correct information that I want the browser to remember. However what the browser actually remembers is the value of the first instance.

我已尝试完全取消设置 cookie,这会导致第二个实例消失,但我所做的一切似乎都无法摆脱第一个实例.我在所有主要浏览器(Chrome、Firefox、IE)中都出现了相同的行为,这让我怀疑我一定在这里做错了什么,但我不确定它是什么.

I've tried unsetting the cookie entirely, which causes the second instance to disappear, but nothing I do seems to get rid of the first instance. I get the same behavior in all major browsers (Chrome, Firefox, IE), which makes me suspect that I must be doing something fundamentally wrong here, but I'm not sure what it is.

您可以在此处查看网站本身.或点击此处通过Facebook 链接(应生成卡住的 cookie).非常感谢任何帮助.

You can view the site itself here. Or click here to access it via a Facebook link (should generate a stuck cookie). Any help is much appreciated.

更新:

因此可靠地重现错误的步骤如下:

So the steps to reliably reproduce the error are as follows:

  1. 通过 Facebook 样式的链接访问该网站
  2. 对布局进行一些更改,然后关闭选项卡.
  3. 通过普通网址访问网站.
  4. 您应该正确记住初次访问时的布局,因此请更改一些内容,然后刷新页面.当页面重新加载时,您的更改将不再被记住.

我还注意到,通过 Facebook 风格的 URL 重新访问该网站能够清除/重置卡住的 cookie.所以这就像浏览器为每个 URL 路径或其他东西保留一个单独的 cookie,并且不允许根页面访问在另一个 URL 路径上设置的 cookie.我想我可以通过在 cookie 上显式设置 path=/ 来解决这个问题,但没有骰子.

I've also noticed that revisiting the site via the Facebook-style URL is able to clear/reset the stuck cookie. So it's like the browser is keeping a separate cookie for each URL path, or something, and not allowing the root page to access the cookie that was set on the other URL path. I thought I might be able to fix this by explicitly setting path=/ on the cookie, but no dice.

更新 2:

我发现如果我同时设置 cookie 的路径和域,我会在所有浏览器中得到不同的行为:

I've found that if I set both the path and the domain of the cookie I get different behavior in all browsers:

  1. Firefox - 现在可以正常工作了,万岁! 可以正常工作一次,然后就坏了,嘘!
  2. Chrome - 无变化
  3. IE - 似乎为每个 URL 保留单独的 cookie,因此 Facebook 样式的 URL 记住一种状态,而标准 URL 记住不同的状态.两者都正确且相互独立地更新.这有点时髦,但仍然比卡住/损坏状态好得多.
  1. Firefox - Works correctly now, hooray! Worked correctly once, then broke, boo!
  2. Chrome - No change
  3. IE - Seems to be keeping separate cookies for each URL, so the Facebook-style URL remembers one state, and the standard URL remembers a different state. Both update correctly and independently of each other. This is kind of funky, but still way better than the stuck/broken state.

推荐答案

Dude(tte),您的 cookie setter 中存在不一致和错误.

Dude(tte), there are inconsistencies, and a bug, in your cookie setter.

清除cookie和设置cookie的路径和域应该相同.在此处查看您的代码:

The path and domain should be the same for both clearing the cookie and setting it. See your code here:

document.cookie = c_name + "=; expires=Fri, 31 Dec 1999 23:59:59 GMT;";

并将其与:

var c_value=escape(value) + "; expires=" + exdate.toUTCString(); + "; path=/spring; domain=aroth.no-ip.org";

你会看到 setter 有两个,但 deleter 没有.你会带来混乱.

you will see that the setter has both of them, but the deleter doesn't. You will bring about chaos.

我上面引用的第二行代码,在字符串连接表达式中间引入了一个分号.紧接在 exdate.toUTCString() 之后.杀死它.杀了它……现在.

That second line of code I quoted above, has a semicolon introduced in the middle of a string concatenation expression. Right after exdate.toUTCString(). Kill it. Kill it…now.

至少在我的谷歌浏览器上,我设法让它正确运行,如果我在 json = "[" + json + "]"; 设置断点并修改 setCookie 在它被执行之前.

At least on my Google Chrome, I managed to get it run correctly, if I set a breakpoint at json = "[" + json + "]"; and modify setCookie before it is executed.

P/S:这是一次奇怪的调试体验,我设法设置了 4 个 layoutState cookie,通过摆弄路径 &域.

P/S: It was a bizzare debugging experience, where I managed to set 4 layoutState cookies, by fiddling with path & domain.

这篇关于Cookie 设置两次;如何删除重复项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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