The usage analysis of the session method in thinkphp3.x _php example

Source: Internet
Author: User
Tags session id php template sessions smarty template

This example describes the use of the session method in thinkphp3.x. Share to everyone for your reference, specific as follows:

The session method is used for session setting, getting, deleting, and managing operations. session for session setup, get, delete, and manage actions

Usage session ($name, $value = ')

Parameter name (required): If an incoming array indicates session initialization, if passing in null indicates emptying the current session, a string represents the session assignment, fetch, or operation.

Value (optional): The session value to set, if passing in NULL indicates a session deletion, the default is an empty string

The return value returns a different value based on the specific usage

The session function is a multiplication function, in which different parameters can be invoked to perform different function operations, including the following functions.

Second, session initialization settings

If the name parameter of the session method is passed in an array, the session initialization setting is represented, for example:

Session (Array (' name ' => ' session_id ', ' expire ' =>3600));

The incoming session parameters are supported including:

ID session_id Value
Name Session_name value
Path Session_save_path Value
Prefix Session localized space prefix
Expire Session.gc_maxlifetime Set Value
Domain Session.cookie_domain Set Value
Use_cookies Session.use_cookies Set Value
Use_trans_sid Session.use_trans_sid Set Value
Cache_limiter Session_cache_limiter Set Value
Cache_expire Session_cache_expire Set Value
Type session hander types, you can use the hander Drive extension

The session initialization method does not need to be called manually, and will be invoked automatically after the initialization of the app class is done, usually the project only needs to configure the Session_options parameter, the session_options parameter setting is an array, The supported index names are the same as the previous session initialization parameters.

By default, the system starts the session automatically after initialization, and you can set Session_auto_start to False if you do not want the system to start the session automatically, for example:

' Session_auto_start ' =>false

Turn off the public files that can be project after automatic startup or start the session in the Controller by manually calling Session_Start or session (' [Start] ').

Third, the session assigned value

Session assignment is relatively simple, direct use:

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

Equivalent:

$_session[' name ' = ' value ';

Four, the session takes the value

Session value using:

$value = Session (' name ');

Equivalent to use:

$value = $_session[' name '];

Five, session deletion

Session (' name ', null); Delete Name

Equivalent:

unset ($_session[' name '));

To remove all sessions, you can use:

session (NULL); Empty the current session

Equivalent:

$_session = Array ();

Six, the session judgment

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

Session ('? name ');

Used to determine if the session value named name is already set

Equivalent:

Isset ($_session[' name '));

Seven, Session Management

The session method supports some simple session management operations, using the following:

Session (' [Operation name] ');

The supported operation names include:

Start session
Pause Pause Session Write
Destroy Destroy session
Regenerate session ID Regeneration

Use the example below:

Session (' [Pause] '); Pause Session Write Session 
(' [start] ');/start session session 
(' [Destroy] ');/Destroy session 
session (' [ Regenerate] "; Regenerate session ID

VIII. Localized support

Localized session management support can be enabled if you pass in the prefix parameter when initializing session settings, or if you set the Session_prefix parameter individually. When you start a localized session, all assignment, value, delete, and judgment operations automatically support localized sessions.

After localization session support is turned on, the generated session data format is changed from the original $_session[' name ' to the $_session[' prefix ' [' name ']
Assuming that the prefix is set to?

Assignment action:

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

Equivalent:

$_session[' It ' [' name '] = ' value ';

Value-taking operation:

$value = Session (' name ');

Equivalent to use:

$value = $_session[' It ' [' name '];

Delete action:

Session (' name ', null);

Equivalent:

unset ($_session[', ' name ']);

Empty operation:

session (NULL); 

Equivalent:

unset ($_session[') ";

Judgment action:

Session ('? name ');

Equivalent:

Isset ($_session[', ' name ']);

PS: Here recommend a few of the format of this site landscaping tools, I believe that we can use in future development:

PHP code online format Landscaping tools:
Http://tools.jb51.net/code/phpformat

JavaScript code Landscaping/compression/formatting/encryption Tools:
http://tools.jb51.net/code/jscompress

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

JSON Code Formatting Landscaping tool:
Http://tools.jb51.net/code/json

Online Xml/json Mutual Conversion tool:
Http://tools.jb51.net/code/xmljson

JSON code online Format/beautify/compress/edit/Convert tools:
Http://tools.jb51.net/code/jsoncodeformat

SQL code Online formatting Landscaping tools:
Http://tools.jb51.net/code/sqlcodeformat

More interested in thinkphp related content readers can view the site topics: "thinkphp Introductory Course", "thinkphp Common Methods Summary", "PHP Cookie Usage Summary", "Smarty Template Introductory Course" and "PHP template technology Summary."

I hope this article will help you with the PHP program design based on thinkphp framework.

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.