假设我有一个网站叫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吗?
当前回答
这是我用过的。注意,这个cookie是在open (http)中传递的,因此是不安全的。我不用它做任何需要安全的事情。
站点A生成一个令牌,并将其作为URL参数传递给站点B。 站点B接受令牌并将其设置为会话cookie。
您可以添加加密/签名来确保安全。好好研究一下如何正确地做到这一点。
其他回答
无法为另一个域设置cookie。
如果要将数据传递到另一个域,可以将其编码到url中。
a.com -> b.com/redirect?info=some+info (and set cookie) -> b.com/other+page
也许你可以使用Iframe。Facebook可能使用了这种技术。你可以在这里阅读更多。Stackoverflow使用了类似的技术,但使用了HTML5本地存储,更多信息请参阅他们的博客
在本链接中,我们将找到解决方案链接。
setcookie("TestCookie", "", time() - 3600, "/~rasmus/", "b.com", 1);
不能为其他域设置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");
?>
看到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的解决方案是可行的,尽管由于其不安全,不建议使用。