thinkphp5.1 cookie跨域、thinkphp5.1 session跨域、tp5.1cookie跨域
cookie跨域:
//config/cookie.php
return [
//...
//仅7.3.0及以上适用
'samesite' => 'None',
//是否加密cookie值,false为不加密
'aeskey' => '1234'
];
//thinkphp/library/think/Cookie.php
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace think;
use think\facade\Log;
class Cookie
{
/**
* 配置参数
* @var array
*/
protected $config = [
// cookie 名称前缀
'prefix' => '',
// cookie 保存时间
'expire' => 0,
// cookie 保存路径
'path' => '/',
// cookie 有效域名
'domain' => '',
// cookie 启用安全传输
'secure' => false,
// httponly设置
'httponly' => false,
// 是否使用 setcookie
'setcookie' => true,
// 加密KEY
'aeskey' => false
];
/**
* 构造方法
* @access public
*/
public function __construct(array $config = [])
{
$this->init($config);
}
/**
* Cookie初始化
* @access public
* @param array $config
* @return void
*/
public function init(array $config = [])
{
$this->config = array_merge($this->config, array_change_key_case($config));
if (!empty($this->config['httponly']) && PHP_SESSION_ACTIVE != session_status()) {
ini_set('session.cookie_httponly', 1);
}
}
public static function __make(Config $config)
{
return new static($config->pull('cookie'));
}
private function aes($str, $type = true)
{
if ($type) {
$str = time() . $this->config['expire'] . '_tp_' . $str;
return openssl_encrypt($str, 'AES-128-ECB', $this->config['aeskey']);
}
$str = openssl_decrypt($str, 'AES-128-ECB', $this->config['aeskey']);
$array = explode('_tp_', $str);
if (count($array) != 2) {
return false;
}
$time = substr($array[0], 0, 10);
$expire = substr($array[0], 10);
if ($expire && time() - $time >= $expire) {
return $expire;
}
return $array[1];
}
/**
* 设置或者获取cookie作用域(前缀)
* @access public
* @param string $prefix
* @return string|void
*/
public function prefix($prefix = '')
{
if (empty($prefix)) {
return $this->config['prefix'];
}
$this->config['prefix'] = $prefix;
}
/**
* Cookie 设置、获取、删除
*
* @access public
* @param string $name cookie名称
* @param mixed $value cookie值
* @param mixed $option 可选参数 可能会是 null|integer|string
* @return void
*/
public function set($name, $value = '', $option = null)
{
// 参数设置(会覆盖黙认设置)
if (!is_null($option)) {
if (is_numeric($option)) {
$option = ['expire' => $option];
} elseif (is_string($option)) {
parse_str($option, $option);
}
$config = array_merge($this->config, array_change_key_case($option));
} else {
$config = $this->config;
}
$name = $config['prefix'] . $name;
// 设置cookie
if (is_array($value)) {
// array_walk_recursive($value, [$this, 'jsonFormatProtect'], 'encode');
// $value = 'think:' . json_encode($value);
$value = serialize($value);
}
$value = urlencode($this->config['aeskey'] ? $this->aes($value) : $value);
// $value = $this->config['aeskey'];
Log::write($this->config, 'error');
$expire = !empty($config['expire']) ? $_SERVER['REQUEST_TIME'] + intval($config['expire']) : 0;
if ($config['setcookie']) {
$this->setCookie($name, $value, $expire, $config);
}
$_COOKIE[$name] = $value;
}
/**
* Cookie 设置保存
*
* @access public
* @param string $name cookie名称
* @param mixed $value cookie值
* @param array $option 可选参数
* @return void
*/
protected function setCookie($name, $value, $expire, $option = [])
{
Log::write($option, 'error');
if ($option['aeskey']) {
unset($option['aeskey']);
}
if (request()->isMobile()) {
$option['domain'] = '';
}
if (version_compare(PHP_VERSION, '7.3.0', '>=')) {
setcookie($name, $value, [
'expires' => $expire,
'path' => $option['path'],
'domain' => $option['domain'],
'secure' => $option['secure'],
'httponly' => $option['httponly'],
'samesite' => $option['samesite'],
]);
} else {
setcookie($name, $value, $expire, $option['path'], $option['domain'], $option['secure'], $option['httponly']);
// setcookie($name, $value, $expire, $path, $domain, $secure, $httponly);
}
}
/**
* 永久保存Cookie数据
* @access public
* @param string $name cookie名称
* @param mixed $value cookie值
* @param mixed $option 可选参数 可能会是 null|integer|string
* @return void
*/
public function forever($name, $value = '', $option = null)
{
if (is_null($option) || is_numeric($option)) {
$option = [];
}
$option['expire'] = 315360000;
$this->set($name, $value, $option);
}
/**
* 判断Cookie数据
* @access public
* @param string $name cookie名称
* @param string|null $prefix cookie前缀
* @return bool
*/
public function has($name, $prefix = null)
{
$prefix = !is_null($prefix) ? $prefix : $this->config['prefix'];
$name = $prefix . $name;
return isset($_COOKIE[$name]);
}
/**
* Cookie获取
* @access public
* @param string $name cookie名称 留空获取全部
* @param string|null $prefix cookie前缀
* @return mixed
*/
public function get($name = '', $prefix = null)
{
$prefix = !is_null($prefix) ? $prefix : $this->config['prefix'];
$key = $prefix . $name;
if ('' == $name) {
if ($prefix) {
$value = [];
foreach ($_COOKIE as $k => $val) {
if (0 === strpos($k, $prefix)) {
$value[$k] = $val;
}
}
} else {
$value = $_COOKIE;
}
} elseif (isset($_COOKIE[$key])) {
$value = $_COOKIE[$key];
$str = urldecode($value);
$str = $this->config['aeskey'] ? $this->aes($str, false) : $str;
try {
$value = unserialize($str);
} catch (Exception $e) {
$value = $str;
}
// $value = $this->config['aeskey'];
/* if (0 === strpos($value, 'think:')) {
$value = substr($value, 6);
$value = json_decode($value, true);
array_walk_recursive($value, [$this, 'jsonFormatProtect'], 'decode');
} */
} else {
$value = null;
}
return $value;
}
/**
* Cookie删除
* @access public
* @param string $name cookie名称
* @param string|null $prefix cookie前缀
* @return void
*/
public function delete($name, $prefix = null)
{
$config = $this->config;
$prefix = !is_null($prefix) ? $prefix : $config['prefix'];
$name = $prefix . $name;
if ($config['setcookie']) {
$this->setcookie($name, '', $_SERVER['REQUEST_TIME'] - 3600, $config);
}
// 删除指定cookie
unset($_COOKIE[$name]);
}
/**
* Cookie清空
* @access public
* @param string|null $prefix cookie前缀
* @return void
*/
public function clear($prefix = null)
{
// 清除指定前缀的所有cookie
if (empty($_COOKIE)) {
return;
}
// 要删除的cookie前缀,不指定则删除config设置的指定前缀
$config = $this->config;
$prefix = !is_null($prefix) ? $prefix : $config['prefix'];
if ($prefix) {
// 如果前缀为空字符串将不作处理直接返回
foreach ($_COOKIE as $key => $val) {
if (0 === strpos($key, $prefix)) {
if ($config['setcookie']) {
$this->setcookie($key, '', $_SERVER['REQUEST_TIME'] - 3600, $config);
}
unset($_COOKIE[$key]);
}
}
}
return;
}
private function jsonFormatProtect(&$val, $key, $type = 'encode')
{
if (!empty($val) && true !== $val) {
$val = 'decode' == $type ? urldecode($val) : urlencode($val);
}
}
}
session跨域
//config/session.php
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// +----------------------------------------------------------------------
// | 会话设置
// +----------------------------------------------------------------------
return [
//...
//samesite None Lax
'samesite' => 'None'
];
//thinkphp/library/think/Session.php
//在官方源代码里的第170行下边增加代码
if (isset($config['secure'])) {
ini_set('session.cookie_secure', $config['secure']);
if (isset($config['samesite'])) {
ini_set('session.cookie_samesite', $config['samesite']);
}
}
版权声明:原源代码版权属于thinkphp官方,其他博主修改代码的版权属于博主个人,修改的代码仅供参考(改的位置有点多,懒得注释了),复制代码产生的一切问题由使用者自行承担。本文未经许可禁止转载、复制、重新发布,否则保留追究版权法律责任!
thinkphp5.1 cookie跨域、thinkphp5.1 session跨域、tp5.1cookie跨域的更多相关文章
- Cookie、Session、Token与JWT(跨域认证)
之前看到群里有人问JWT相关的内容,只记得是token的一种,去补习了一下,和很久之前发的认证方式总结的笔记放在一起发出来吧. Cookie.Session.Token与JWT(跨域认证) 什么是Co ...
- web跨域访问,session丢失的问题
web跨域访问,session丢失的问题25 http://www.iteye.com/problems/71265 http://www.iteye.com/topic/264079 具体情况如下: ...
- 前端笔记之服务器&Ajax(下)数据请求&解决跨域&三级联动&session&堆栈
一.请求后端的JSON数据 JSON是前后端通信的交互格式,JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式. JSON是互联网各个后台与 ...
- cookie,Session机制的本质,跨应用程序的session共享
目录:一.术语session二.HTTP协议与状态保持三.理解cookie机制四.理解session机制五.理解javax.servlet.http.HttpSession六.HttpSession常 ...
- 前端Js跨域方法汇总—剪不断,理还乱,是跨域
1.通过jsonp跨域2.通过修改document.domain来跨子域(iframe)3.隐藏的iframe+window.name跨域4.iframe+跨文档消息传递(XDM)5.跨域资源共享 C ...
- 跨服务器之间的session共享
跨服务器之间的Session共享方案需求变得迫切起来,最终催生了多种解决方案,下面列举4种较为可行的方案进行对比探讨: 1. 基于NFS的Session共享 NFS是Net FileSystem的简称 ...
- PHP--浏览器禁用cookie后,怎么使用session
sessionid是存储在cookie中的,解决方案如下: Session URL重写,保证在客户端禁用或不支持COOKIE时,仍然可以使用Session session机制.session机制是一种 ...
- PHP跨页面传递时session失效
一直都是使用wamp作为本地的PHP集成开发环境 今天遇到一个很奇怪的问题,就是在跨页面传递时session竟然失效了,而之前从来没有出现过这种问题 因为使用的是开源的php框架为了测试方便就新建了两 ...
- 关于用户禁用Cookie的解决办法和Session的图片验证码应用
当用户通过客户端浏览页面初始化了Session之后(如:添加购物车,用户登陆等),服务器会将这些session数据保存在:Windows保存在C:\WINDOWS\Temp的目录下,Linux则是保存 ...
随机推荐
- 本地使用 Docker Compose 与 Nestjs 快速构建基于 Dapr 的 Redis 发布/订阅分布式应用
Dapr(分布式应用程序运行时)介绍 Dapr 是一个可移植的.事件驱动的运行时,它使任何开发人员能够轻松构建出弹性的.无状态和有状态的应用程序,并可运行在云平台或边缘计算中,它同时也支持多种编程语言 ...
- Windows JDK 的下载与安装
Java Development Kit 简称 JDK,任何需要开发 Java 程序的环境都需要进行安装 JDK. JDK 下载地址:https://www.oracle.com/java/techn ...
- Identity Server 4使用OpenID Connect添加用户身份验证(三)
一.说明 基于上一篇文章中的代码进行继续延伸,只需要小小的改动即可,不明白的地方可以先看看本人上一篇文章及源码: Identity Server 4资源拥有者密码认证控制访问API(二) GitHub ...
- C#常见的集合
3中数组式的 Array 在内存上是连续分配的,而且元素类型是一样的 特点:读取快,可以坐标访问,增删慢.长度不变. ArrayList 不定长,连续分配的,元素没有类型限制,任何元素都当成Objec ...
- Go语言基础二:常用的Go工具命令
常用的Go工具命令 Go附带了一下有用的命令,这些命令可以简化开发的过程.命令通常包含的IDE中,从而使工具在整个开发环境中保持一致. go run 命令 go run命令实在开发过程中执行的最常见的 ...
- 在半小时内从无到有开发并调试一款Chrome扩展(Chrome插件/谷歌浏览器插件)
原文转载自「刘悦的技术博客」https://v3u.cn/a_id_120 就在不久之前,我们目前这个毕业班的班长那日同学和我说,他正在公司开发Chrome扩展,看起来很高大上的技术,实际开发却非常简 ...
- SElinux管理
SElinux: 是Linux的一个强制访问控制的安全模块 SElinux的相关概念: 对象:文件.目录.进程.端口等 主体:进程称为主体 SElinux将所有的文件都赋予一个type类型的标签,所有 ...
- EPLAN中的edz文件的用法
1 EDZ 文件的定义 EDZ 是 EPLAN Data Archive Zipped(EPLAN 数据压缩文件包)的缩写,最早是专门为西门子定制的,现在已经 成为 EPLAN 中一种标准的部件 ...
- 我分析30w条数据后发现,西安新房公摊最低的竟是这里?
前两天一个邻居发出了灵魂质问:"为什么我买的180平和你的169平看上去一样大?" "因为咱俩的套内面积都是138平......" 我们去看房子,比较不同楼盘的 ...
- Luogu1919 【模板】A*B Problem升级版(FFT)
简单的\(A*B\) \(Problem\),卡精度卡到想女装 #include <iostream> #include <cstdio> #include <cstri ...