如何为其他网域设置Cookie [英] How to set a cookie for another domain
问题描述
假设我有一个名为 a.com
的网站,当网站的特定网页载入时,表示网页连结,我想为其他网站设定Cookie调用 b.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
并将用户重定向到 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?
推荐答案
p>您无法为其他网域设置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屋!