SoFunction
Updated on 2025-04-04

Detailed explanation of the usage of session method in thinkPHP

This article describes the usage of the session() method in thinkPHP. Share it for your reference, as follows:

The system provides complete support for Session management and operation, and all operations can be completed through a built-in session function.

usage

session(name,name,value='')

parameter

name (must): If the array is passed in, it means session initialization, if the null is passed in, it means session assignment, obtaining or operation.
Value (optional): The session value to be set. If null is passed in, it means deleting the session, and the default is an empty string.

session initialization settings

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

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

After initialization, the system will automatically start the session. If you do not want the system to start the session automatically, you can set SESSION_AUTO_START to false

For example:

'SESSION_AUTO_START' =>false

Close the public files of the project after automatic startup or start the session by manually calling session_start or session('[start]') in the controller.

session assignment

session('name','value'); //Set session

session value

$value = session('name');

session deletion

session('name',null); // Delete name

To delete all sessions, you can use:

session(null); // Clear the current session

Session judgment

To determine whether a session value has been set, you can use

session('?name');

Used to determine whether the session value with name name has been set

session management

The session method supports some simple session management operations, and the usage is as follows:

session('[operation name]');
session('[pause]'); // Pause session writingsession('[start]'); // Start sessionsession('[destroy]'); // Destroy sessionsession('[regenerate]'); // Regenerate session id

PHP comes with its own function session_unset.The function is to clear (release) all SESSION (session) variables. If you need to clear a certain session variable, you should use it.

unset($_SESSION['var']);

For more information about thinkPHP, please visit the special topic of this site:ThinkPHP Introduction Tutorial》、《Summary of the operation skills of thinkPHP templates》、《Summary of common methods of ThinkPHP》、《Codeigniter Introductory Tutorial》、《Advanced tutorial on CI (CodeIgniter) framework》、《Zend FrameWork framework tutorial》、《Basic tutorial on getting started with smarty templates"and"PHP template technical summary》。

I hope that the description in this article will be helpful to everyone's PHP programming based on the ThinkPHP framework.