如何为另一个域设置 cookie [英] How to set a cookie for another domain
问题描述
假设我有一个名为 a.com
的网站,当加载该网站的特定页面时,比如页面链接,我喜欢为另一个名为 b 的网站设置 cookie.com
,然后将用户重定向到 b.com
.
Say I have a website called a.com
, and when a specific page of this site is loaded, say page link, I like to set a cookie for another site called b.com
, then redirect the user to b.com
.
我的意思是,在加载 a.com/link
时,我想为 b.com
设置一个 cookie 并将用户重定向到 b.com代码>.
I mean, on load of a.com/link
I want to set a cookie for b.com
and redirect user to b.com
.
我对其进行了测试,并且浏览器实际上从 a.com/link
收到了 cookie,但它并没有在重定向请求中将该 cookie 发送到 b.com
.正常吗?
I tested it, and browser actually received the cookie from a.com/link
, but it didn't send that cookie on the redirection request to b.com
. Is it normal?
我们可以为其他域设置 cookie 吗?
Can we set cookies for other domains?
推荐答案
您不能为其他域设置 cookie.允许这样做会带来巨大的安全漏洞.
You cannot set cookies for another domain. Allowing this would present an enormous security flaw.
您需要获取 b.com 才能设置 cookie.如果 a.com 将用户重定向到 b.com/setcookie.php?c=value
You need to get b.com to set the cookie. If a.com redirect the user to b.com/setcookie.php?c=value
setcookie 脚本可以包含以下内容来设置 cookie 并重定向到 b.com 上的正确页面
The setcookie script could contain the following to set the cookie and redirect to the correct page on b.com
<?php
setcookie('a', $_GET['c']);
header("Location: b.com/landingpage.php");
?>
这篇关于如何为另一个域设置 cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!