Thinkphp Summary _php instance of operation method about session

Source: Internet
Author: User

This article details the thinkphp on the session of the various methods of operation, details are as follows:

Thinkphp operation session The official documentation is as follows:

Start session
Pause Pause Session
Clear clears session
Destroy Destroy session
Get gets session value
Getlocal Get Private Session value
Set setting session Value
SetLocal Set Private Session value
Name Gets or sets the Session_name
Is_set whether to set session value
Is_setlocal whether to set private session values
ID GET or set session_id
Path Gets or sets the Session_save_path
Setexpire Set Session Expiration time
Setcookiedomain set a valid domain name
Setcallback sets the callback function when the session object is deserialized

The most common method of action sample code is as follows:

Detects whether the session variable exists
session::is_set (' name ');
Assign a value to the session variable
session::set (' name ', ' value ');
Gets the session variable
session::get (' name ');

The configuration parameter code associated with the session is as follows:

' Session_name ' => ' thinkid ',        //default Session_name
' Session_path ' => ',//            Use default session save PATH
' Session_type ' => ' File ',/            /default session type supports DB and File 
' session_expire ' => ' 300000 ',        //default session validity
' session_table ' => ' think_session ',    //Database Session Way Table name
' session_callback ' => ',            // Callback methods for deserializing objects

Where the Session_name parameter requires attention, if you need to not share the value of the delivery session between different items, set a different value, or leave the same default value.
If you set the same Session_name value, but you want to create a private session space based on your project, what should you do with it? Thinkphp also supports private session operations with project as session space, taking the previous common operations for example, we change the following:

Detects if the session variable exists (the current project is valid)
session::is_setlocal (' name ');
Assign value to session variable (current project valid)
session::setlocal (' name ', ' value ');
Gets the session variable (current project valid)
session::getlocal (' name ');

This will not conflict with the global session operation and can be used for special situations.
thinkphp Support Database mode of session operation, set Session_type value of DB on it, if you use the database method, but also to ensure that the value of session_table set, and import the following DDL to your Database (in MySQL for example):

CREATE TABLE ' think_session ' (
' id ' int (one) unsigned not null auto_increment,
' session_id ' varchar (255) is not NULL,
' session_expires ' int (one) not NULL,
' Session_data ' blob,
PRIMARY KEY (' id ')


Note that theDB session database connection is connected using the project's database configuration information. In addition to the database method, can also add other ways of the session save mechanism, such as memory mode, memcache way, etc., we just add the appropriate filter on the line, using the Session_set_save_handler method, The specific method defines the reference Think.Util.Filter the implementation of the FilterSessionDb.class.php file below.

Made a simple landing judgment
The session value is given after the login detection so that the value of the session is non-null false

$_session[c (' User_auth_key ')] = $logInFind [' id '];

where [C (' User_auth_key ')] is the thinkphp built-in method and function class. Null defaults when config.php files are not configured
The $loginfind[' ID ' is taken out of the account value assigned to it, the default is to close the page session automatically deleted disappear!
Other pages use the following format to judge

 if (!isset ($_session[c (' User_auth_key '))) {//isset is to detect whether the variable is assigned! $this->redirect (' login ', ' login '); Go to registration page} 

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.