当前位置:网站首页>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' => '/'
]);
边栏推荐
- JS interview collection test question 1
- 案例分享|智慧化的西部机场
- Collectors.groupingBy 排序
- 在{{}}中拼接字符
- Simple and practical accounting software, so that accounts can be checked
- Fabric.js IText设置指定文字的颜色和背景色
- Draw a wave chart_ Digital IC
- el form 表单validate成功后没有执行逻辑
- Practical problem solving ability of steam Education
- Gee: find the spatial distribution and corresponding time of the "greenest" in the Yellow River Basin in 2020 [pixel by pixel analysis]
猜你喜欢
Summary of database problems
【pyinstaller】_get_sysconfigdata_name() missing 1 required positional argument: ‘check_exists‘
Solution: the agent throws an exception error
[opencv] image binarization
LS1046nfs挂载文件系统
创新永不止步——nVisual网络可视化平台针对Excel导入的创新历程
Differential identities (help find mean, variance, and other moments)
Gee series: unit 8 time series analysis in Google Earth engine [time series]
[bus interface] Axi interface
The underlying principle of go map (storage and capacity expansion)
随机推荐
leetcode存在重复元素go实现
Go Chan's underlying principles
Gee: analyze the change of spatial centroid of remote sensing image [centroid acquisition analysis]
Express logistics quick query method, set the unsigned doc No. to refresh and query automatically
7.1模擬賽總結
[common error] the DDR type of FPGA device is selected incorrectly
Knowledge arrangement about steam Education
Fabric.js 圆形笔刷
Gee series: unit 8 time series analysis in Google Earth engine [time series]
画波形图_数字IC
Collectors.groupingBy 排序
Dark horse notes -- Set Series Collection
2022 Alibaba global mathematics competition, question 4, huhushengwei (blind box problem, truck problem) solution ideas
黑马笔记---Set系列集合
Feign realizes file uploading and downloading
摆正元素(带过渡动画)
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
黑馬筆記---Set系列集合
Mapping settings in elk (8) es
LeetCode 241. Design priorities for operational expressions (divide and conquer / mnemonic recursion / dynamic programming)