当前位置:网站首页>php/js cookie共享跨域的问题
php/js cookie共享跨域的问题
2022-07-02 05:10:00 【为天空着色】
记录一下最近做的一个cookie共享的需求.,有两种情况:
第一种:相同的顶级域名的情况下,只需要将cookie写在顶级域名下,该域名下的所有子域名都能访问到了。如 PHP:
//xxx.com 前面不能加. (生成的cookie的domain是 .xxx.com)
setcookie('test','value',time()+60*60*24*30,'/','xxxx.com');
//xxx.com 的所有子域名就 都能获取到了
$_COOKIE['test'];
JS的设置和php类似:
//存储cookie,这里的域名必须是顶级域名
setCookie('test','value','xxx.com','20')
function setCookie(cName, value,domain,expireDate) {
const exDate = new Date();
exDate.setDate(exDate .getDate() + expireDate);
document.cookie = cName + "=" + decodeURIComponent(value) + (expireDate== null ? "" : ";expires=" + exDate.toUTCString()) + ";path=/;domain="+domain;
}
//获取cookie
getCookie('test')
function getCookie(key) {
return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(key).replace(/[-.+*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
}
第二种:两个顶级域名不同的站点,需要在设置cookie的时候设置httpOnly、secure、sameSite;比如A设置了cookie,B可以通过jsonp的方式就能取到了。sameSite必须设置为None,默认是空。如PHP:
php>=7.3版本可以直接设置
setcookie('test','value',[
'expires'=>time()+60*60*24*30,
'path'=>'/',
'domain'=>'xxx.com',
'httponly'=>true,
'secure'=>true,
'samesite'=>'None'
]);
php<7.3
function samesite_setcookie($name, $value, array $options)
{
$header = 'Set-Cookie:';
$header .= rawurlencode($name) . '=' . rawurlencode($value) . ';';
if (isset($options['expires'])) {
$header .= 'expires=' . \gmdate('D, d-M-Y H:i:s T', $options['expires']) . ';';
}
if (isset($options['expires'])) {
$header .= 'Max-Age=' . max(0, (int) ($options['expires'] - time())) . ';';
}
if (!empty($options['path'])) {
$header .= 'path=' . $options['path']. ';';
}
if (!empty($options['domain'])) {
$header .= 'domain=' . rawurlencode($options['domain']) . ';';
}
if (!empty($options['secure'])) {
$header .= 'Secure;';
}
if (!empty($options['httponly'])) {
$header .= 'HttpOnly;';
}
if (!empty($options['samesite'])) {
$header .= 'SameSite=' . rawurlencode($options['samesite']);
}
header($header, false);
$_COOKIE[$name] = $value;
}
samesite_setcookie('test', 'value', [
'expires' => time()+60*60*24*30,
'domain' => 'xxx.com',
'httponly' => true,
'samesite' => 'None',
'secure' => true,
'path' => '/'
]);
边栏推荐
- 将光盘中的cda保存到电脑中
- Fabric.js 居中元素
- leetcode两数相加go实现
- go实现leetcode旋转数组
- Mouse events in JS
- Fabric.js 精简JSON
- Pycharm breakpoint management: temporarily cancel some breakpoints + run directly to a line
- Use of typescript classes
- Creation and destruction of function stack frames
- Video multiple effects production, fade in effect and border background are added at the same time
猜你喜欢
数学知识——快速幂的理解及例题
How do I interview for a successful software testing position? If you want to get a high salary, you must see the offer
Fabric.js 精简JSON
C# 基于MQTTNet的服务端与客户端通信案例
10 minute quick start UI automation ----- puppeter
视差特效的原理和实现方法
06 decorator mode
Change deepin to Alibaba image source
el-cascader回显只选中不显示的问题
LM09丨费雪逆变换反转网格策略
随机推荐
运维工作的“本手、妙手、俗手”
The reason why sizeof (ARR) / sizeof (arr[0]) is used in the function to calculate the length of the array is incorrect
农业生态领域智能机器人的应用
No logic is executed after the El form is validated successfully
Pytest learning ----- pytest assertion of interface automation testing
Mathematical problems (number theory) trial division to judge prime numbers, decompose prime factors, and screen prime numbers
C # picture display occupancy problem
Fabric.js 居中元素
Acelems Expressway microgrid energy efficiency management platform and intelligent lighting solution intelligent lighting tunnel
Paddlepaddle project source code
Rhcsa --- work on the third day
6.30年终小结,学生时代结束
Pyechart1.19 national air quality exhibition
MMAP zero copy knowledge point notes
LeetCode 241. Design priorities for operational expressions (divide and conquer / mnemonic recursion / dynamic programming)
Mathematical knowledge (Euler function)
数学知识——快速幂的理解及例题
Steam教育的实际问题解决能力
Express logistics quick query method, set the unsigned doc No. to refresh and query automatically
案例分享|智慧化的西部机场