假设我有一个网站叫a.com,当这个网站的一个特定页面被加载时,比如说页面链接,我想为另一个网站b.com设置一个cookie,然后将用户重定向到b.com。

我的意思是,在a.com/link的负载上,我想为b.com设置一个cookie并将用户重定向到b.com。

我测试了一下,浏览器实际上收到了来自a.com/link的cookie,但它没有在重定向请求时将该cookie发送到b.com。这正常吗?

我们可以为其他域设置cookie吗?


当前回答

如果你有a.my-company.com和b.my-company.com,而不是只有a.com和b.com,你可以为。my-company.com域名发布一个cookie -它将被接受并发送到两个域名。

其他回答

与上面的答案相似,但不是重定向到页面,然后再返回,这会导致糟糕的用户体验,您可以在域a上设置图像。

<img src="http://www.example.com/cookie.php?val=123" style="display:none;">

然后在域B, cookie。php中的example.com,你会有如下代码:

<?php
    setcookie('a', $_GET['val']);
?>

向苏宾致敬

如果你有a.my-company.com和b.my-company.com,而不是只有a.com和b.com,你可以为。my-company.com域名发布一个cookie -它将被接受并发送到两个域名。

无法为另一个域设置cookie。

如果要将数据传递到另一个域,可以将其编码到url中。

a.com  ->  b.com/redirect?info=some+info (and set cookie) -> b.com/other+page

在本链接中,我们将找到解决方案链接。

setcookie("TestCookie", "", time() - 3600, "/~rasmus/", "b.com", 1);

看到RFC6265:

The user agent will reject cookies unless the Domain attribute specifies a scope for the cookie that would include the origin server. For example, the user agent will accept a cookie with a Domain attribute of "example.com" or of "foo.example.com" from foo.example.com, but the user agent will not accept a cookie with a Domain attribute of "bar.example.com" or of "baz.foo.example.com". NOTE: For security reasons, many user agents are configured to reject Domain attributes that correspond to "public suffixes". For example, some user agents will reject Domain attributes of "com" or "co.uk". (See Section 5.3 for more information.)

但是上面提到的image/iframe的解决方案是可行的,尽管由于其不安全,不建议使用。