Home > PHP Framework > ThinkPHP > What is the usage of session in thinkphp5

What is the usage of session in thinkphp5

WBOY
Release: 2022-04-25 12:04:52
Original
4287 people have browsed it

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='')
Copy after login

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));
Copy after login

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
Copy after login

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
Copy after login

is equivalent to:

$_SESSION['name'] = 'value';
Copy after login

session take Value

Session value use:

$value = session('name');
Copy after login

is equivalent to use:

$value = $_SESSION['name'];
Copy after login

session deletion

session('name',null); // 删除name
Copy after login

is equivalent to:

unset($_SESSION['name']);
Copy after login

To delete all sessions, you can use:

session(null); // 清空当前的session
Copy after login

Equivalent to:

$_SESSION = array();
Copy after login

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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template