当前位置:网站首页>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' => '/'
]);
边栏推荐
- 创新永不止步——nVisual网络可视化平台针对Excel导入的创新历程
- Global and Chinese market of insulin pens 2022-2028: Research Report on technology, participants, trends, market size and share
- Getting started with pytest ----- confitest Application of PY
- Oracle和MySQL的基本区别(入门级)
- 农业生态领域智能机器人的应用
- Pyflink writes MySQL examples with JDBC
- 在{{}}中拼接字符
- 農業生態領域智能機器人的應用
- Fabric.js 更换图片的3种方法(包括更换分组内的图片,以及存在缓存的情况)
- Domestic all Chinese automatic test software apifox
猜你喜欢
LeetCode 1175. Prime number arrangement (prime number judgment + Combinatorial Mathematics)
Pytest learning ----- pytest Interface Association framework encapsulation of interface automation testing
Pyechats 1.19 generate a web version of Baidu map
Preparation for writing SAP ui5 applications using typescript
CubeMx DMA笔记
Hcip day 17
LM09丨费雪逆变换反转网格策略
Mathematical knowledge (Euler function)
视差特效的原理和实现方法
Super detailed pycharm tutorial
随机推荐
js中的Map(含leetcode例题)
从数组中找出和为目标的下标
6.30 year end summary, end of student age
ansible安装与使用
视差特效的原理和实现方法
leetcode两数相加go实现
创新永不止步——nVisual网络可视化平台针对Excel导入的创新历程
Map in JS (including leetcode examples)
Hcip day 17
Using Kube bench and Kube hunter to evaluate the risk of kubernetes cluster
国产全中文-自动化测试软件Apifox
Feign realizes file uploading and downloading
Feign realizes file uploading and downloading
[quick view opencv] familiar with CV matrix operation with image splicing examples (3)
Save the CDA from the disc to the computer
Analyzing the hands-on building tutorial in children's programming
Pyechart1.19 national air quality exhibition
Oracle和MySQL的基本区别(入门级)
摆正元素(带过渡动画)
Fabric.js IText 手动设置斜体