当前位置:网站首页>Php/js cookie sharing across domains
Php/js cookie sharing across domains
2022-07-02 05:17:00 【Color the sky】
Record a recent cookie The need for sharing ., There are two situations :
The first one is : In the case of the same top-level domain name , Only need to cookie Write it under the top-level domain name , All subdomains under this domain name can be accessed . Such as PHP:
//xxx.com You can't add . ( Generated cookie Of domain yes .xxx.com)
setcookie('test','value',time()+60*60*24*30,'/','xxxx.com');
//xxx.com All subdomains of Can get
$_COOKIE['test'];JS The settings and php similar :
// Storage cookie, The domain name here must be a top-level domain name
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;
}
// obtain cookie
getCookie('test')
function getCookie(key) {
return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(key).replace(/[-.+*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
}The second kind : Two sites with different top-level domains , It needs to be set in cookie When setting httpOnly、secure、sameSite; such as A Set up cookie,B Can pass jsonp You can get it by the way of .sameSite Must be set to None, The default is an empty . Such as PHP:
php>=7.3 Version can be set directly
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' => '/'
]);边栏推荐
- fastText文本分类
- Global and Chinese market of hydrocyclone desander 2022-2028: Research Report on technology, participants, trends, market size and share
- Fabric.js 居中元素
- Gee series: unit 8 time series analysis in Google Earth engine [time series]
- Go Chan's underlying principles
- 7.1模拟赛总结
- Exercise notes 13 (effective letter ectopic words)
- Feign realizes file uploading and downloading
- Collectors.groupingBy 排序
- Black Horse Notes - - set Series Collection
猜你喜欢

Go Chan's underlying principles

运维工作的“本手、妙手、俗手”

Summary of database problems

Latest: the list of universities and disciplines for the second round of "double first-class" construction was announced

Pycharm breakpoint management: temporarily cancel some breakpoints + run directly to a line

函数栈帧的创建和销毁

Gee series: unit 6 building various remote sensing indexes in Google Earth engine

Collectors. Groupingby sort
![[high speed bus] Introduction to jesd204b](/img/78/1a0a3672e63058da6d98da95aa3cf2.jpg)
[high speed bus] Introduction to jesd204b

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
随机推荐
Exercise notes 13 (effective letter ectopic words)
Gee series: Unit 1 Introduction to Google Earth engine
Knowledge arrangement about steam Education
7.1 Résumé du concours de simulation
设置滚动条默认样式 谷歌浏览器
[opencv] image binarization
Fabric.js 自由绘制矩形
Gee series: unit 7 remote sensing image classification using GEE [random forest classification]
Mathematical problems (number theory) trial division to judge prime numbers, decompose prime factors, and screen prime numbers
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
Cubemx DMA notes
Go implements leetcode rotation array
操作符详解
Set the default style of scroll bar Google browser
leetcode两数相加go实现
Summary of database problems
LS1046nfs挂载文件系统
How to make an RPM file
Fabric.js 精简JSON
运维工作的“本手、妙手、俗手”