The session method is used for session setup, get, delete, and manage operations.
| Session for session settings, get, delete, and manage operations |
| Usage |
Session ($name, $value = ") |
| Parameters |
Name (required): If the incoming array is a session initialization, if NULL is passed in to empty the current session, the string indicates the session assignment, fetch, or operation.
Value (optional): The session value to be set, if the null is passed in to delete the session, the default is an empty string |
| return value |
See details (return different values depending on the specific usage) |
The session function is a diversified operation function, which can accomplish different functions by passing in different parameter calls, including some of the following functions. [-more-]
Session Initialization settings
If the name parameter of the session method is passed in the array, the session initialization setting is used, for example:
- Session(array(' name '= 'session_id ',' expire '=3600));
Copy Code
Support for incoming session parameters includes:
| 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 values |
| use_cookies |
session.use_cookies setting value |
| use_trans_sid |
session.use_trans_sid setting value |
| cache_limiter |
session_cache_limiter setting value |
| cache_expire |
session_cache_expire setting value |
| Type |
session hander type, can use hander driver extension |
SESSION initialization setting method does not need to call manually, after the initialization of the app class is called automatically, usually the project only needs to configure the Session_options parameter, the Session_options parameter is set to 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 Code
Turn off the session by manually invoking Session_Start or session (' [Start] ') on the project's public file or in the controller after auto-start.
Session Assignment
The session assignment is relatively straightforward and is used directly:
- Session(' name ',' value '); //Set session
Copy Code
Equivalent:
- $_session[' name '] = ' value ';
Copy Code Session value
Session value using:
- $value = session(' name ');
Copy Code
Equivalent to using:
- $value = $_session[' name '];
Copy Code Session Delete
- Session(' name ',null); //Remove name
Copy Code
Equivalent:
- unset($_session[' name ']);
Copy Code
To delete all the sessions, you can use:
- Session(null); //Clear the current session
Copy Code
Equivalent:
- $_session = array();
Copy Code session judgment
To determine if a session value has been set, you can use the
- Session('? name ');
Copy Code
Used to determine if the session value named name is already set
Equivalent:
- Isset($_session[' name ']);
Copy Code Session Management
The session method supports some simple session management operations with the following usage:
- Session(' [operation name] ');
Copy Code
The supported operation names are:
| Action name |
meaning |
| Start |
Start session |
| Pause |
Pause Session Write |
| Destroy |
Destroy session |
| Regenerate |
Regenerate session ID |
Examples of use are:
- Session(' [pause] '); //Pause session write
- Session(' [start] '); //Start session
- Session(' [Destroy] '); //Destroy session
- Session(' [regenerate] '); //Regenerate session ID
Copy Code localization support
Localized session management support can be enabled if you pass in the prefix parameter when initializing the session setting or if the Session_prefix parameter is set individually. After the localization session is started, all assignments, values, deletions, and judgments will automatically support localization sessions.
After localization session support is turned on, the session data format generated by the original
- $_session[' name '] becomes $_session[' prefix '] [' name ']
Copy Code
Assuming the prefix is set to think, the assignment operation:
- Session(' name ',' value '); //Set session
Copy Code
Equivalent:
- $_session[' think '] [' name '] = ' value ';
Copy Code
To take a value action:
- $value = session(' name ');
Copy Code
Equivalent to using:
- $value = $_session[' think ' ['name '];
Copy Code
Delete operation:
- Session(' name ',null);
Copy Code
Equivalent:
- unset($_session[' think '] [' name ']);
Copy Code
Clear operation:
- Session(null);
Copy Code
Equivalent:
- unset($_session[' think ']);
Copy Code
To determine the operation:
- Session('? name ');
Copy Code
Equivalent:
- Isset($_session[' think '] [' name ']);
Copy Code
The session usage of thinkphp