The Session method is used to set, obtain, delete, and manage sessions. The Session method is used to set, obtain, delete, and manage sessions.
Sessions are used to set, obtain, delete, and manage sessions. |
Usage |
Session ($ name, $ value = '') |
Parameters |
Name (required): If an array is input, session initialization is performed. If null is input, the current session is cleared. if it is a string, the session value is assigned, obtained, or operated. Value (optional): The session Value to be set. If null is input, the session is deleted. the default Value is a null string. |
Return value |
See details (return different values based on specific usage) |
The session function is a diverse operation function. you can call different parameters to complete different function operations, including the following functions. [-More-] session initialization setting if the name parameter of the session method is input into an array, it indicates the session initialization setting, for example:
- Session (array ('name' => 'session _ id', 'expire '=> 3600 ));
The following session parameters supported by the copy code include:
Parameter name |
Description |
Id |
Session_id value |
Name |
Session_name value |
Path |
Session_save_path value |
Prefix |
Session localized space prefix |
Expire |
Session. gc_maxlifetime setting value |
Domain |
Session. cookie_domain setting value |
Use_cookies |
Session. use_cookies setting value |
Use_trans_sid |
Session. use_trans_sid setting value |
Cache_limiter |
Session_cache_limiter |
Cache_expire |
Session_cache_expire |
Type |
Session hander type, which can be extended using the hander driver |
The Session initialization setting method does not need to be manually called. it is automatically called after the initialization of the App class ends. Generally, the project only needs to configure the SESSION_OPTIONS parameter. the SESSION_OPTIONS parameter is set as an array, the index names supported are the same as the previous session initialization parameters.
By default, the system automatically starts 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 the code to close the public file of the project after automatic startup, or manually call session_start or session ('[start]') in the controller to start the session. Session assignment:
- Session ('name', 'value'); // sets the session
Copying code is equivalent:
- $ _ SESSION ['name'] = 'value ';
Copy code session value Session value usage:
- $ Value = session ('name ');
Copying code is equivalent to using:
- $ Value = $ _ SESSION ['name'];
Copy code session deletion
- Session ('name', null); // delete name
Copying code is equivalent:
- Unset ($ _ SESSION ['name']);
To delete all sessions, copy the code:
- Session (null); // clear the current session
Copying code is equivalent:
- $ _ SESSION = array ();
Copy the code session to determine whether a session value has been set. you can use
- Session ('? Name ');
Copy the code to determine whether the session value with the name has been set
Equivalent:
- Isset ($ _ SESSION ['name']);
The copy code session management session method supports some simple session management operations. the usage is as follows:
- Session ('[operation name]');
The operation names supported by the copy code include:
Operation Name |
Description |
Start |
Start session |
Pause |
Pause session writing |
Destroy |
Destroy session |
Regenerate |
Regenerate session id |
Example:
- Session ('[pause]'); // pause session writing
- Session ('[start]'); // start the session
- Session ('[destroy]'); // destroy the session
- Session ('[regenerate]'); // regenerate the session id
Copy code localization supports enabling local session management if the prefix parameter is input during session initialization or the SESSION_PREFIX parameter is set separately. After a localized session is started, all assignment, value, deletion, and judgment operations automatically support the localized session.
After the local session is enabled, the format of the generated session data is changed from
- $ _ SESSION ['name'] becomes $ _ SESSION ['prefix'] ['name']
If the prefix of the copied code is set to think, the value assignment operation is performed:
- Session ('name', 'value'); // sets the session
Copying code is equivalent:
- $ _ SESSION ['think'] ['name'] = 'value ';
Copy the code value operation:
- $ Value = session ('name ');
Copying code is equivalent to using:
- $ Value = $ _ SESSION ['think'] ['name'];
Copy code delete operation:
- Session ('name', null );
Copying code is equivalent:
- Unset ($ _ SESSION ['think'] ['name']);
Copy the code to clear:
- Session (null );
Copying code is equivalent:
- Unset ($ _ SESSION ['think']);
Copy the code to determine the operation:
- Session ('? Name ');
Copying code is equivalent:
- Isset ($ _ SESSION ['think'] ['name']);
Copy code