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='')".
The operating environment of this article: Windows 10 system, ThinkPHP version 5, Dell G3 computer.
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!