Home PHP Framework ThinkPHP ThinkPHP6.0: Changes in Session and Cookie Mechanisms

ThinkPHP6.0: Changes in Session and Cookie Mechanisms

Dec 16, 2019 pm 02:30 PM
cookie session

 

ThinkPHP6.0: Changes in Session and Cookie Mechanisms

最新的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',
Copy after login

该中间件的作用主要是初始化SessionId,并注入当前的请求对象。初始化的时候会自动读取config目录下的session.php配置。

默认使用文件的方式保存Session数据,支持使用redis、memcache以及memcached。

内置的Session驱动默认配置下,都是支持跨应用读取Session数据的。如果你需要区分不同的应用,保持各个应用的Session数据独立,可以在应用的session配置文件中设置prefix参数,例如:

'prefix' => 'admin',
Copy after login

并且为了防止Session数据过多,文件方式写入Session数据的时候支持垃圾回收机制。可以配置gc_divisor和gc_maxlifetime两个参数来设置GC回收。

Session数据会在当前请求结束后,自动写入,一般只会写入一次。Session数据写入之前会首先进行序列化,默认的序列化方法是serialize/unserialize,你可以在session配置文件中设置serialize参数(数组)来改变默认的序列化机制,例如:

'serialize' => ['json_encode', 'json_decode'],
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Where are cookies stored? Where are cookies stored? Dec 20, 2023 pm 03:07 PM

Cookies are usually stored in the cookie folder of the browser. Cookie files in the browser are usually stored in binary or SQLite format. If you open the cookie file directly, you may see some garbled or unreadable content, so it is best to use Use the cookie management interface provided by your browser to view and manage cookies.

Where are the cookies on your computer? Where are the cookies on your computer? Dec 22, 2023 pm 03:46 PM

Cookies on your computer are stored in specific locations on your browser, depending on the browser and operating system used: 1. Google Chrome, stored in C:\Users\YourUsername\AppData\Local\Google\Chrome\User Data\Default \Cookies etc.

How to solve session failure How to solve session failure Oct 18, 2023 pm 05:19 PM

Session failure is usually caused by the session lifetime expiration or server shutdown. The solutions: 1. Extend the lifetime of the session; 2. Use persistent storage; 3. Use cookies; 4. Update the session asynchronously; 5. Use session management middleware.

Solution to PHP Session cross-domain problem Solution to PHP Session cross-domain problem Oct 12, 2023 pm 03:00 PM

Solution to the cross-domain problem of PHPSession In the development of front-end and back-end separation, cross-domain requests have become the norm. When dealing with cross-domain issues, we usually involve the use and management of sessions. However, due to browser origin policy restrictions, sessions cannot be shared by default across domains. In order to solve this problem, we need to use some techniques and methods to achieve cross-domain sharing of sessions. 1. The most common use of cookies to share sessions across domains

Where are the mobile cookies? Where are the mobile cookies? Dec 22, 2023 pm 03:40 PM

Cookies on the mobile phone are stored in the browser application of the mobile device: 1. On iOS devices, Cookies are stored in Settings -> Safari -> Advanced -> Website Data of the Safari browser; 2. On Android devices, Cookies Stored in Settings -> Site settings -> Cookies of Chrome browser, etc.

How cookies work How cookies work Sep 20, 2023 pm 05:57 PM

The working principle of cookies involves the server sending cookies, the browser storing cookies, and the browser processing and storing cookies. Detailed introduction: 1. The server sends a cookie, and the server sends an HTTP response header containing the cookie to the browser. This cookie contains some information, such as the user's identity authentication, preferences, or shopping cart contents. After the browser receives this cookie, it will be stored on the user's computer; 2. The browser stores cookies, etc.

Detailed explanation of where browser cookies are stored Detailed explanation of where browser cookies are stored Jan 19, 2024 am 09:15 AM

With the popularity of the Internet, we use browsers to surf the Internet have become a way of life. In the daily use of browsers, we often encounter situations where we need to enter account passwords, such as online shopping, social networking, emails, etc. This information needs to be recorded by the browser so that it does not need to be entered again the next time you visit. This is when cookies come in handy. What are cookies? Cookie refers to a small data file sent by the server to the user's browser and stored locally. It contains user behavior of some websites.

What are the differences between JavaScript and PHP cookies? What are the differences between JavaScript and PHP cookies? Sep 02, 2023 pm 12:29 PM

JavaScriptCookies Using JavaScript cookies is the most effective way to remember and track preferences, purchases, commissions and other information. Information needed for a better visitor experience or website statistics. PHPCookieCookies are text files that are stored on client computers and retained for tracking purposes. PHP transparently supports HTTP cookies. How do JavaScript cookies work? Your server sends some data to your visitor's browser in the form of a cookie. Browsers can accept cookies. If present, it will be stored on the visitor's hard drive as a plain text record. Now, when a visitor reaches another page on the site

See all articles