假设我有一个网站叫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的解决方案是可行的,尽管由于其不安全,不建议使用。

其他回答

从a发送POST请求。POST请求只在服务器端,客户端不能访问。

您可以使用CURL(推荐,服务器端)或隐藏方法="POST"表单(客户端)从a.com发送POST请求到b.com。如果选择后者,可能需要对JavaScript进行模糊处理,这样用户就无法理解算法并干扰算法。

在b.com上做一个网关来设置cookie:

<?php
    if (isset($_POST['data']) {
        setcookie('a', $_POST['data']);
        header("Location: b.com/landingpage");
    }
?>

如果想进一步提高安全性,可以在两边(a.com和b.com)实现一个函数,使用加密密码加密(a.com上)和解密(b.com上)数据。

如果您正在尝试做一些必须绝对安全的事情(例如传输登录会话),请尝试oAuth或从https://api.cloudianos.com/docs#v2/auth获得一些灵感

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

也许你可以使用Iframe。Facebook可能使用了这种技术。你可以在这里阅读更多。Stackoverflow使用了类似的技术,但使用了HTML5本地存储,更多信息请参阅他们的博客

看到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);