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

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

问题描述

所以我有一个网站使用cookie来记住访问当前的布局状态。一切都工作很好,直到我添加了一个Facebook的喜欢按钮到网站生成链接,允许用户共享一个特定的UI状态(有点混乱,但不是真正相关的问题)。



问题是,当我通过这些Facebook链接之一访问网站时,我的布局cookie的第二个副本似乎被创建(如,我看到两个饼干相同的名称和不同的值)。这不会太可怕,除了重复的cookie的值似乎被卡住,加上用户返回到网站时,浏览器记住的卡住值而不是最近设置的值(因此它的种类比如有一个好的cookie,我仍然可以使用,一个坏的,我不能,浏览器喜欢记住坏的cookie而不是好的cookie)。这打破了我的布局跟踪/记忆功能。



这里有两个问题:



  1. 如何解决已经有卡住的cookie的任何用户的问题(我知道我可以选择一个新的名称为cookie,


  2. 如果我在访问Chrome浏览器的开发者控制台之后使用Chrome浏览器的开发者控制台页面处于卡住状态,我可以看到 document.cookie 是(为了可读性而添加格式化):

      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' ,'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' '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/'}]$​​ b $ b

    忽略Wibiya cookie和JSESSIONID。卡住的cookie是第一个'layoutState'实例,我仍然可以在JavaScript中操作的是第二个'layoutState'实例。这是我得到的如果我改变一些事情:

      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'有正确的信息,我想让浏览器记住。然而,浏览器实际记住的是第一个实例的值。



    我已经尝试完全取消设置cookie,这导致第二个实例消失,但我似乎没有做任何事情摆脱第一个实例。我在所有主要的浏览器(Chrome,Firefox,IE)中得到相同的行为,这让我怀疑我必须在这里做一些基本上错误,但我不知道是什么。



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



    更新:



    重现错误如下:


    1. 通过Facebook-样式链接

    2. 对布局进行一些更改,然后

    3. 通过正常网址访问网站。

    4. 您的布局从初始访问应该正确记住,所以改变一些事情,然后刷新页面。页面重新载入后,您的更改将不再记住。

    我也注意到,通过Facebook样式重新访问网站URL能够清除/重置卡住的Cookie。因此,就像浏览器为每个网址路径保留一个单独的Cookie,或者某些操作,并且不允许根页面访问在其他网址路径上设置的Cookie。我想我可以通过在cookie上显式设置 path = / 来解决这个问题,但没有骰子。



    更新2:



    我发现如果我同时设置了Cookie的路径和域名, :


    1. Firefox - 现在可以正常工作,hooray!正常工作一次,然后破裂, >
    2. Chrome - 无变化

    3. IE - 似乎要为每个网址保留单独的Cookie,因此Facebook样式的网址会记住一个状态,标准网址会记住不同的状态。两者都正确更新,彼此独立。这是一种时髦,但仍然比卡住/破碎状态更好。


    解决方案

    Dude(tte),在Cookie设置器中有不一致和一个错误。



    1。确保正确设置路径和域



    路径和域应该相同,以清除Cookie并进行设置。请在这里查看您的代码:

      document.cookie = c_name +=; expires = Fri,31 Dec 1999 23:59:59格林威治标准时间;; 

    并与其比较:

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

    你会看到setter有两个,但是删除器不会。你会带来混乱。



    2。哦,那个讨厌的分号



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



    至少在我的Google Chrome浏览器上,我设法让它正常运行,如果我在 json = [+ json +]; 并在执行之前修改 setCookie



    P / S:这是一个bizzare调试经验,其中我设法设置4 layoutState cookie,通过fiddling路径&域。


    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).

    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.

    So there are two questions here:

    1. How do I stop this from happening/why is this happening in the first place?
    2. How do I fix things for any users that already have a stuck cookie (I know I could just pick a new name for the cookie, but I'd rather do it by finding a way to properly unstick the stuck 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/'}]"
    

    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'}]
    

    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.

    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.

    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.

    Update:

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

    1. Visit the site via the Facebook-style link
    2. Make some changes to the layout, and then close the tab.
    3. Visit the site via the normal URL.
    4. Your layout from the initial visit should be correctly remembered, so change some things around and then refresh the page. When the page reloads, your changes will no longer be remembered.

    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.

    Update 2:

    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 - 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), there are inconsistencies, and a bug, in your cookie setter.

    1. Make sure path and domain is correctly set

    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;";
    

    and compare it to:

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

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

    2. Oh, and that nasty semicolon

    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.

    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: It was a bizzare debugging experience, where I managed to set 4 layoutState cookies, by fiddling with path & domain.

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

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