Usage Analysis of the session method in thinkphp3.x, thinkphp3.xsession

Source: Internet
Author: User

Usage Analysis of the session method in thinkphp3.x, thinkphp3.xsession

This example describes the usage of the session method in thinkphp3.x. We will share this with you for your reference. The details are as follows:

1. 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 = '')

Parameter 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 values 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.

Ii. session Initialization settings

If the name parameter of the session method is passed in an array, it indicates that the session Initialization is set. For example:

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

The following session parameters are supported:

Id session_id Value
Name session_name Value
Path session_save_path Value
Prefix session local 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
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

After automatic start is disabled, you can start the session by manually calling session_start or session ('[start]') in the public file of the project or controller.

Iii. Assignment of sessions

Session assignment is relatively simple and can be used directly:

Session ('name', 'value'); // sets the session

Equivalent:

$_SESSION['name'] = 'value';

Iv. session values

Session value usage:

$value = session('name');

Equivalent:

$value = $_SESSION['name'];

5. delete a session

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

Equivalent:

unset($_SESSION['name']);

To delete all sessions, you can use:

Session (null); // clear the current session

Equivalent:

$_SESSION = array();

Vi. session judgment

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

session('?name');

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

Equivalent:

isset($_SESSION['name']);

VII. session management

The session method supports some simple session management operations. The usage is as follows:

Session ('[operation name]');

Supported operation names include:

Start session
Pause session writing
Destroy session
Regenerate to regenerate the session id

Example:

Session ('[pause]'); // pause session writing to the session ('[start]'); // start the session ('[destroy]'); // destroy the session ('[regenerate]'); // regenerate the session id

8. Localization support

If the prefix parameter is input during session Initialization or the SESSION_PREFIX parameter is set separately, the local session management support can be enabled. After a localized session is started, all assignment, value, deletion, and judgment operations automatically support the localized session.

After a localized session is enabled, the format of the generated session data is changed from $ _ SESSION ['name'] to $ _ SESSION ['prefix'] ['name'].
If the prefix is set to think

Assignment operation:

Session ('name', 'value'); // sets the session

Equivalent:

$_SESSION['think']['name'] = 'value';

Value operation:

$value = session('name');

Equivalent:

$value = $_SESSION['think']['name'];

Delete operation:

session('name',null);

Equivalent:

unset($_SESSION['think']['name']);

Clear operation:

session(null); 

Equivalent:

unset($_SESSION['think']);

Judgment operation:

session('?name');

Equivalent:

isset($_SESSION['think']['name']);

PS: We recommend several formatting and beautification tools on this site. I believe you can use them in future development:

Php code online formatting and beautification tools:
Http://tools.jb51.net/code/phpformat

JavaScript code beautification/compression/formatting/encryption tools:
Http://tools.jb51.net/code/jscompress

Online XML formatting/compression tools:
Http://tools.jb51.net/code/xmlformat

JSON code formatting and beautification tools:
Http://tools.jb51.net/code/json

Online XML/JSON conversion tools:
Http://tools.jb51.net/code/xmljson

Json code online formatting/beautification/compression/editing/conversion tools:
Http://tools.jb51.net/code/jsoncodeformat

SQL code online formatting and beautification tools:
Http://tools.jb51.net/code/sqlcodeformat

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.