共享子域cookie

发布于 2025-01-04 18:31:01 字数 418 浏览 1 评论 0原文

我有一个域“www.foo.com”,我想创建子域“test.foo.com”。 为了合并这 2 个域以仅共享一个 cookie,我将 cookie 设置为如下所示:

Cookie cookie = new Cookie("myCookie", "myValue");
cookie.setMaxAge(60 * 60);
cookie.setDomain(".foo.com");

因此从现在开始将只有一个 cookie:“foo.com”,并且这些值将保存在同一个 cookie 上。 问题是对于老用户来说,对于他们来说会有两个cookie('www.foo.com'和'foo.com'),我怎样才能将这两个cookie合并为一个?

另一件事是,来自“test.foo.com”的用户最终将访问“www.foo.com”,反之亦然。

I have a domain 'www.foo.com' and I want to create sub domain 'test.foo.com'.
In order to combine those 2 domains to share only one cookie I set the cookie to be like that:

Cookie cookie = new Cookie("myCookie", "myValue");
cookie.setMaxAge(60 * 60);
cookie.setDomain(".foo.com");

So from now on there will be only one cookie: 'foo.com' and the values will be save on the same cookie.
The problem is for old users, for them there will be two cookies ('www.foo.com' and 'foo.com'), how can i merge those two cookies to one??

One more thing, users from 'test.foo.com' eventually will visit 'www.foo.com' and vise versa.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风吹过旳痕迹 2025-01-11 18:31:01

从 http servlet 请求中获取旧的 cookie,然后将其最大期限设置为 0。这将触发客户端删除它(在其自己的时间内,通常是立即)。另请参阅有关 Cookie 的 Javadoc。

setMaxAge

public void setMaxAge(int expiry) Sets the maximum age in seconds for this Cookie. A positive value indicates that the cookie will expire after that many seconds have passed. Note that the value is the maximum age when the cookie will expire, not the cookie's current age. A negative value means that the cookie is not stored persistently and will be deleted when the Web browser exits. A zero value causes the cookie to be deleted. Parameters: expiry - an integer specifying the maximum age of the cookie in seconds; if negative, means the cookie is not stored; if zero, deletes the cookie See Also: getMaxAge()

您需要解析您的 cookie 并搜索您想要删除的 cookie。像这样的东西:

final Cookie[] cookies = request.getCookies();
for(Cookie cookie: cookies) {
    if("www.foo.com".equals(cookie.getDomain()) cookie.setMaxAge(0);
}

Get the old cookie from the http servlet request, then set its max age to 0. That will trigger the client side to get rid of it (in its own time, normally right away). Also, see the Javadoc on Cookie.

setMaxAge

public void setMaxAge(int expiry) Sets the maximum age in seconds for this Cookie. A positive value indicates that the cookie will expire after that many seconds have passed. Note that the value is the maximum age when the cookie will expire, not the cookie's current age. A negative value means that the cookie is not stored persistently and will be deleted when the Web browser exits. A zero value causes the cookie to be deleted. Parameters: expiry - an integer specifying the maximum age of the cookie in seconds; if negative, means the cookie is not stored; if zero, deletes the cookie See Also: getMaxAge()

You will need to parse through your cookies and search for the one you are trying to get rid of. Something like this:

final Cookie[] cookies = request.getCookies();
for(Cookie cookie: cookies) {
    if("www.foo.com".equals(cookie.getDomain()) cookie.setMaxAge(0);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文