首页 > 解决方案 > 在跨源请求中将域设置为客户端主机的 cookie

问题描述

当从 example1.com 到 example2.com 进行 api 调用时,服务器(example2.com)是否可以将 cookie 设置为域为“example1.com”的客户端(example1.com)

标签: node.jsxmlcookiescross-domain

解决方案


我认为不可能将 cookie 从一个域设置到另一个域,因为如果发生这种情况,安全性就会存在很多缺陷。

您需要获取 example2.com 来设置 cookie。如果 example1.com 将用户重定向到 example2.com/setcookie.php?example3=value

setcookie 脚本可以包含以下内容以设置 cookie 并重定向到 example2.com 上的正确页面

<?php
    setcookie('example1', $_GET['example3']);
    header("Location: example2.com/landingpage.php");
?>

更多访问:

https://stackoverflow.com/questions/6761415/how-to-set-a-cookie-for-another-domain#:~:text=Setting%20cookies%20for%20another%20domain,encode%20this%20into%20the %20url.&text=你%20can't%2C%20at%20least,be%20a%20nasty%20security%20risk

关于类似主题的讨论很好。


推荐阅读