ThinkPHP跨域设置”samesite=none”和“secure”参数的方法和注意事项

本文介绍如何在ThinkPHP框架中实现跨域请求并同步Cookie,包括设置Access-Control-Allow-Credentials及SameSite属性等内容。

开发板推荐:天空星STM32F407VET6开发板

超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印

因开发插件需要,将另一个网站的数据通过AJAX添加到ThinkPHP制作 的一个网站上的购物车。
需要网站支持跨域请求同步COOKE,具体操作如下:
/public/index.php
第二行添加以下代码:
ACAO=′∗′;header("Access−Control−Allow−Credentials:true");//允许COOKIE共享header("XDomainRequestAllowed:1");//允许COOKIE共享IEif(!empty(ACAO = '*'; header("Access-Control-Allow-Credentials: true"); //允许COOKIE共享 header("XDomainRequestAllowed: 1"); //允许COOKIE共享IE if(!empty(ACAO=;header("AccessControlAllowCredentials:true");//COOKIEheader("XDomainRequestAllowed:1");//COOKIEIEif(!empty(_SERVER[‘HTTP_ORIGIN’])) $ACAO = $_SERVER[‘HTTP_ORIGIN’] ;
header(“Access-Control-Allow-Origin: $ACAO”);
// header(“Access-Control-Allow-Origin: *”);
header(“Access-Control-Allow-Credentials: true”);//是否允许后续请求携带认证信息(cookies),该值只能是true,否则不返回
header(“Access-Control-Allow-Methods: GET, POST, OPTIONS”);
header(“Access-Control-Allow-Headers: Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With, X-Ds-Key”);
header(“P3P: CP=CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR”);

/thinkphp/library/think/Cookie.php
COOKIE[_COOKIE[COOKIE[name] = $value;下添加

KaTeX parse error: Expected '}', got 'EOF' at end of input: cookie = "{var}={KaTeX parse error: Expected 'EOF', got '}' at position 6: value}̲"; if(time) cookie.=";expires=".gmstrftime("cookie .= "; expires=".gmstrftime("%A, %d-%b-%Y %H:%M:%S GMT",cookie.=";expires=".gmstrftime("time + 86400)."";
if($path) KaTeX parse error: Expected '}', got 'EOF' at end of input: …ie .= "; path={path}";
if($host) KaTeX parse error: Expected '}', got 'EOF' at end of input: … .= "; domain={host}";
$cookie .= “; SameSite=None”;
KaTeX parse error: Expected '}', got 'EOF' at end of input: …("Set-Cookie: {cookie}");

即可。
注意事项:

跨域Cookie要求带SameSite=None; Secure
而Secure属性要求当前页面必须是HTTPS
所以在HTTP下发送带Secure参数的Cookie会被浏览器忽略
导致不是https时登录或语言切换设置Cookie无法记录
而在https下切换语言和登录会正常.
解决方案:
在以下文件添加代码
/application/当前模板名/index/view/common/meta.html

开发板推荐:天空星STM32F407VET6开发板

超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值