当前位置:网站首页>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' => '/'
]);边栏推荐
- 黑马笔记---Map集合体系
- 4. Flask cooperates with a tag to link internal routes
- 数学知识(欧拉函数)
- C case of communication between server and client based on mqttnet
- 函数中使用sizeof(arr) / sizeof(arr[0])求数组长度不正确的原因
- Dark horse notes -- Set Series Collection
- Pyflink writes MySQL examples with JDBC
- 在{{}}中拼接字符
- Mathematical knowledge -- understanding and examples of fast power
- Briefly introduce chown command
猜你喜欢

解析少儿编程中的动手搭建教程

2022-003arts: recursive routine of binary tree

Go Chan's underlying principles

How do I interview for a successful software testing position? If you want to get a high salary, you must see the offer

数学知识(欧拉函数)

Rhcsa --- work on the fourth day

Rhcsa --- work on the third day

Fabric.js 渐变

Dark horse notes -- map set system

CubeMx DMA笔记
随机推荐
Fabric.js 渐变
解决:代理抛出异常错误
黑马笔记---Map集合体系
How to configure PostgreSQL 12.9 to allow remote connections
Ruby replaces gem Alibaba image
2022 Alibaba global mathematics competition, question 4, huhushengwei (blind box problem, truck problem) solution ideas
Mathematical knowledge -- understanding and examples of fast power
MMAP zero copy knowledge point notes
CubeMx DMA笔记
fastText文本分类
About PROFIBUS: communication backbone network of production plant
Pyflink writes MySQL examples with JDBC
4. Flask cooperates with a tag to link internal routes
关于Steam 教育的知识整理
黑马笔记---Set系列集合
画波形图_数字IC
[bus interface] Axi interface
7.1 Résumé du concours de simulation
Solution: the agent throws an exception error
leetcode两数相加go实现