search
HomePHP FrameworkThinkPHPWhat is the usage of session in thinkphp5

In thinkphp5, the session method is used for setting, obtaining, deleting and managing the Session. It is a diversified operation function that can be called with different parameters to complete different functional operations. The syntax is "session ($name, $value='')".

What is the usage of session in thinkphp5

The operating environment of this article: Windows 10 system, ThinkPHP version 5, Dell G3 computer.

What is the usage of session in thinkphp5

Session method is used for Session setting, retrieval, deletion and management operations.

Usage

session($name, $value='')

Parameter name (required): If an array is passed in, it means session initialization. If null is passed in, it means clearing the current session. If it is a string, then Represents session assignment, acquisition or operation.

Value (optional): The session value to be set. If null is passed in, it means deleting the session. The default is an empty string.

The session function is a diversified operation function. Pass in different Parameter calling can complete different functional operations, including the following functions.

session initialization setting

If the name parameter of the session method is passed into the array, it means session initialization setting, for example:

session(array('name'=>'session_id','expire'=>3600));

Session initialization setting method There is no need to call it manually. It will be called automatically after the initialization of the App class. Usually the project only needs to configure the SESSION_OPTIONS parameter. The setting of the SESSION_OPTIONS parameter is an array. The supported index names are the same as the previous session initialization parameters.

By default, the system will automatically start the session after initialization. If you do not want the system to automatically start the session, you can set SESSION_AUTO_START to false, for example:

'SESSION_AUTO_START' =>false

After turning off automatic startup, you can use the project's public File or manually start the session by calling session_start or session('[start]') in the controller.

session assignment

Session assignment is relatively simple, use it directly:

session('name','value');  //设置session

is equivalent to:

$_SESSION['name'] = 'value';

session take Value

Session value use:

$value = session('name');

is equivalent to use:

$value = $_SESSION['name'];

session deletion

session('name',null); // 删除name

is equivalent to:

unset($_SESSION['name']);

To delete all sessions, you can use:

session(null); // 清空当前的session

Equivalent to:

$_SESSION = array();

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What is the usage of session in thinkphp5. For more information, please follow other related articles on the PHP Chinese website!

Statement
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

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.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools