当前位置:网站首页>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' => '/'
]);边栏推荐
- Super detailed pycharm tutorial
- Pycharm breakpoint management: temporarily cancel some breakpoints + run directly to a line
- Fabric.js 右键菜单
- Differential identities (help find mean, variance, and other moments)
- Change deepin to Alibaba image source
- Ruby replaces gem Alibaba image
- 國產全中文-自動化測試軟件Apifox
- js面试收藏试题1
- There are duplicate elements in leetcode. Go implementation
- 解析少儿编程中的动手搭建教程
猜你喜欢

函数栈帧的创建和销毁
![[bus interface] Axi interface](/img/ee/95ade7811ec2c37fb67a77f0b6ae2a.jpg)
[bus interface] Axi interface

视差特效的原理和实现方法

数据库问题汇总

培养中小学生对教育机器人的热爱之心

LeetCode 241. Design priorities for operational expressions (divide and conquer / mnemonic recursion / dynamic programming)

Precipitate yourself and stay up late to sort out 100 knowledge points of interface testing professional literacy

Knowledge arrangement about steam Education

Cultivate primary and secondary school students' love for educational robots

Fabric.js 右键菜单
随机推荐
Orthogonal test method and function diagram method for test case design
Global and Chinese markets for marine selective catalytic reduction systems 2022-2028: Research Report on technology, participants, trends, market size and share
培养中小学生对教育机器人的热爱之心
Fabric.js IText设置指定文字的颜色和背景色
Pyechats 1.19 generate a web version of Baidu map
Record my pytorch installation process and errors
Pytest learning ----- pytest assertion of interface automation testing
Leetcode basic programming: array
Preparation for writing SAP ui5 applications using typescript
Find the subscript with and as the target from the array
Fabric.js IText 手动设置斜体
Leetcode18题 【四数之和】递归解法
Case sharing | intelligent Western Airport
奠定少儿编程成为基础学科的原理
About PROFIBUS: communication backbone network of production plant
Steam教育的实际问题解决能力
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 圆形笔刷
Cultivate primary and secondary school students' love for educational robots
06 装饰(Decorator)模式