假设我有一个网站叫a.com,当这个网站的一个特定页面被加载时,比如说页面链接,我想为另一个网站b.com设置一个cookie,然后将用户重定向到b.com。
我的意思是,在a.com/link的负载上,我想为b.com设置一个cookie并将用户重定向到b.com。
我测试了一下,浏览器实际上收到了来自a.com/link的cookie,但它没有在重定向请求时将该cookie发送到b.com。这正常吗?
我们可以为其他域设置cookie吗?
假设我有一个网站叫a.com,当这个网站的一个特定页面被加载时,比如说页面链接,我想为另一个网站b.com设置一个cookie,然后将用户重定向到b.com。
我的意思是,在a.com/link的负载上,我想为b.com设置一个cookie并将用户重定向到b.com。
我测试了一下,浏览器实际上收到了来自a.com/link的cookie,但它没有在重定向请求时将该cookie发送到b.com。这正常吗?
我们可以为其他域设置cookie吗?
当前回答
看到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的解决方案是可行的,尽管由于其不安全,不建议使用。
其他回答
在本链接中,我们将找到解决方案链接。
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的解决方案是可行的,尽管由于其不安全,不建议使用。
你不能,但是…如果你拥有这两个页面,那么……
1)可以通过查询参数(http://siteB.com/?key=value)发送数据
2)你可以在站点A内创建一个站点B的iframe,你可以从一个地方发送帖子到另一个地方。由于站点B是站点B cookie的所有者,它将能够通过处理正确的帖子消息来设置您需要的任何值。(你应该阻止其他不想要的发件人向你发送消息!这取决于你和你决定使用的机制来防止这种情况发生)
你不能,至少不能直接。这将是一个严重的安全风险。
虽然可以指定Domain属性,但规范说:“用户代理将拒绝cookie,除非Domain属性为cookie指定了包括原始服务器的范围。”
由于源服务器是a.com,不包括b.com,所以无法设置。
您需要获取b.com来设置cookie。您可以通过(例如)HTTP重定向到b.com并返回。
不能为其他域设置cookie。允许这样做会带来巨大的安全漏洞。
您需要获取b.com来设置cookie。如果是a.com,将用户重定向到b.com/setcookie.php?c=value
setcookie脚本可以包含以下内容来设置cookie并重定向到b.com上的正确页面
<?php
setcookie('a', $_GET['c']);
header("Location: b.com/landingpage.php");
?>