当前位置:网站首页>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' => '/'
]);边栏推荐
- Mysql重点难题(2)汇总
- Dark horse notes -- Set Series Collection
- How to make an RPM file
- 操作符详解
- 國產全中文-自動化測試軟件Apifox
- LeetCode 241. Design priorities for operational expressions (divide and conquer / mnemonic recursion / dynamic programming)
- Fabric.js 精简JSON
- Find the subscript with and as the target from the array
- Gee: find the spatial distribution and corresponding time of the "greenest" in the Yellow River Basin in 2020 [pixel by pixel analysis]
- C case of communication between server and client based on mqttnet
猜你喜欢

Analyzing the hands-on building tutorial in children's programming

黑马笔记---Set系列集合

How to configure PostgreSQL 12.9 to allow remote connections
![[opencv] image binarization](/img/7e/b56a59ffae3bf6cac9c0bb7e090b85.jpg)
[opencv] image binarization

LeetCode 1175. 质数排列(质数判断+组合数学)

Super detailed pycharm tutorial

Nodejs (03) -- custom module

Cubemx DMA notes

Mathematical knowledge (Euler function)
![Gee series: Unit 5 remote sensing image preprocessing [GEE grid preprocessing]](/img/1e/cf0aa09c2fce2278386f12eae4a6cd.jpg)
Gee series: Unit 5 remote sensing image preprocessing [GEE grid preprocessing]
随机推荐
Fabric.js IText 上标和下标
Gee series: unit 10 creating a graphical user interface using Google Earth engine [GUI development]
Mathematical problems (number theory) trial division to judge prime numbers, decompose prime factors, and screen prime numbers
函数栈帧的创建和销毁
About PROFIBUS: communication backbone network of production plant
Global and Chinese markets of semiconductor laser therapeutics 2022-2028: Research Report on technology, participants, trends, market size and share
Super detailed pycharm tutorial
Fabric.js 渐变
js面试收藏试题1
Solution: the agent throws an exception error
Fabric.js 激活输入框
Fabric.js IText 手动设置斜体
Fabric.js 基础笔刷
Fabric.js 自由绘制矩形
leetcode存在重复元素go实现
从数组中找出和为目标的下标
Global and Chinese market of travel data recorder (VDR) 2022-2028: Research Report on technology, participants, trends, market size and share
运维工作的“本手、妙手、俗手”
数据的储存
Knowledge arrangement about steam Education