因开发插件需要,将另一个网站的数据通过AJAX添加到ThinkPHP制作 的一个网站上的购物车。 需要网站支持跨域请求同步COOKE,具体操作如下: /public/index.php 第二行添加以下代码:
A
C
A
O
=
′
?
′
;
h
e
a
d
e
r
(
"
A
c
c
e
s
s
?
C
o
n
t
r
o
l
?
A
l
l
o
w
?
C
r
e
d
e
n
t
i
a
l
s
:
t
r
u
e
"
)
;
/
/
允
许
C
O
O
K
I
E
共
享
h
e
a
d
e
r
(
"
X
D
o
m
a
i
n
R
e
q
u
e
s
t
A
l
l
o
w
e
d
:
1
"
)
;
/
/
允
许
C
O
O
K
I
E
共
享
I
E
i
f
(
!
e
m
p
t
y
(
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 的
C
O
O
K
I
E
[
_COOKIE[
C?OOKIE[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)
c
o
o
k
i
e
.
=
"
;
e
x
p
i
r
e
s
=
"
.
g
m
s
t
r
f
t
i
m
e
(
"
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
|