当前位置:网站首页>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' => '/'
]);边栏推荐
- C# 图片显示占用问题
- Analyzing the hands-on building tutorial in children's programming
- Find the subscript with and as the target from the array
- Application d'un robot intelligent dans le domaine de l'agroécologie
- 数学知识——快速幂的理解及例题
- Express logistics quick query method, set the unsigned doc No. to refresh and query automatically
- 数据库问题汇总
- paddle: ValueError:quality setting only supported for ‘jpeg‘ compression
- How to make an RPM file
- Global and Chinese market of travel data recorder (VDR) 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢

No logic is executed after the El form is validated successfully

2022 Alibaba global mathematics competition, question 4, huhushengwei (blind box problem, truck problem) solution ideas

Differential identities (help find mean, variance, and other moments)

Rhcsa --- work on the third day

Fabric.js IText 上标和下标

06 decorator mode

Paddlepaddle project source code

摆正元素(带过渡动画)

Preparation for writing SAP ui5 applications using typescript

Fabric.js IText设置指定文字的颜色和背景色
随机推荐
Practical problem solving ability of steam Education
Fabric.js 右键菜单
How do I interview for a successful software testing position? If you want to get a high salary, you must see the offer
Creation and destruction of function stack frames
The underlying principle of go map (storage and capacity expansion)
Summary of database problems
Line by line explanation of yolox source code of anchor free series network (7) -- obj in head_ loss、Cls_ Loss and reg_ Calculation and reverse transmission of loss I
06 decorator mode
Fabric.js 激活输入框
Mathematical knowledge -- understanding and examples of fast power
解析少儿编程中的动手搭建教程
Essence and physical meaning of convolution (deep and brief understanding)
ansible安装与使用
Implementation of go language for deleting duplicate items in sorting array
函数栈帧的创建和销毁
Case sharing | intelligent Western Airport
Pytest learning ----- pytest assertion of interface automation testing
Lay the foundation for children's programming to become a basic discipline
4. Flask cooperates with a tag to link internal routes
Implementation of leetcode two number addition go