最新的6.0版本对框架的Session和Cookie机制进行了重新设计,摆脱了原有设计的束缚。
Session
新的Session机制不依赖PHP内置的Session会话机制,完全独立实现,优势在于可以兼容任何的运行环境,而且配置参数也大为简化。
新版你不能再使用$_SESSION或者任何php内置的session函数操作Session,必须通过think\Session类或者think\facade\Session静态代理类操作Session数据。
Session对于很多API接口应用来说,不是必须的,而随着前后端分离的应用架构设计越来越多,因此6.0的Session默认是关闭的,系统把Session相关的功能设计成一个独立的中间件,如果你需要使用Session可以单独在应用的中间件定义文件中开启。
默认安装后,在app目录下的middleware.php中间件定义文件中,对可能需要使用的系统中间件预置了注释,你可以直接取消注释,加上:
// Session初始化 'think\\middleware\\SessionInit',
该中间件的作用主要是初始化SessionId,并注入当前的请求对象。初始化的时候会自动读取config目录下的session.php配置。
默认使用文件的方式保存Session数据,支持使用redis、memcache以及memcached。
内置的Session驱动默认配置下,都是支持跨应用读取Session数据的。如果你需要区分不同的应用,保持各个应用的Session数据独立,可以在应用的session配置文件中设置prefix参数,例如:
'prefix' => 'admin',
并且为了防止Session数据过多,文件方式写入Session数据的时候支持垃圾回收机制。可以配置gc_divisor和gc_maxlifetime两个参数来设置GC回收。
Session数据会在当前请求结束后,自动写入,一般只会写入一次。Session数据写入之前会首先进行序列化,默认的序列化方法是serialize/unserialize,你可以在session配置文件中设置serialize参数(数组)来改变默认的序列化机制,例如:
'serialize' => ['json_encode', 'json_decode'],
Session类本身的用法和之前版本基本一致,增加了push方法用于追加一个session数组。
Cookie
Cookie的存取机制是分开设计的,读取操作是通过$_COOKIE读取,写入则通过可扩展的方式满足不同运行环境的Cookie写入要求。Cookie数据的写入操作也是在当前请求发送响应数据之前统一写入。
但在实际使用中,Cookie的用法和之前类似,区别在于不再支持前缀配置和清空操作。
PHP中文网,大量的免费ThinkPHP入门教程,欢迎在线学习!
php中文网学习专题:php session (包含图文、视频、案例)
本文转自:https://blog.thinkphp.cn/1077719
The above is the detailed content of ThinkPHP6.0: Changes in Session and Cookie Mechanisms. For more information, please follow other related articles on the PHP Chinese website!

The article discusses ThinkPHP's built-in testing framework, highlighting its key features like unit and integration testing, and how it enhances application reliability through early bug detection and improved code quality.

Article discusses using ThinkPHP for real-time stock market data feeds, focusing on setup, data accuracy, optimization, and security measures.

The article discusses key considerations for using ThinkPHP in serverless architectures, focusing on performance optimization, stateless design, and security. It highlights benefits like cost efficiency and scalability, but also addresses challenges

The article discusses implementing service discovery and load balancing in ThinkPHP microservices, focusing on setup, best practices, integration methods, and recommended tools.[159 characters]

ThinkPHP's IoC container offers advanced features like lazy loading, contextual binding, and method injection for efficient dependency management in PHP apps.Character count: 159

The article discusses using ThinkPHP to build real-time collaboration tools, focusing on setup, WebSocket integration, and security best practices.

ThinkPHP benefits SaaS apps with its lightweight design, MVC architecture, and extensibility. It enhances scalability, speeds development, and improves security through various features.

The article outlines building a distributed task queue system using ThinkPHP and RabbitMQ, focusing on installation, configuration, task management, and scalability. Key issues include ensuring high availability, avoiding common pitfalls like imprope


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use