Usage analysis of the session method in thinkphp3.x, thinkphp3.xsession _ PHP Tutorial

Source: Internet
Author: User
Usage analysis of the session method in thinkphp3.x, thinkphp3.xsession. The usage of the session method in thinkphp3.x. thinkphp3.xsession describes the usage of the session method in thinkphp3.x. I. usage analysis of the session method in S 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.