因开发插件需要,将另一个网站的数据通过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("Access−Control−Allow−Credentials:true");//允许COOKIE共享header("XDomainRequestAllowed:1");//允许COOKIE共享IEif(!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
本文介绍如何在ThinkPHP框架中实现跨域请求并同步Cookie,包括设置Access-Control-Allow-Credentials及SameSite属性等内容。

5757

被折叠的 条评论
为什么被折叠?



