ThinkPHP framework custom SESSION processing example (1/3)

Source: Internet
Author: User
Tags extend mixed redis

Comrades who use ThinkPHP should know that in ThinkPHP, the database can be used to process sessions. The specific implementation process is as follows:

ThinkPHP custom SESSION-driven database storage

In ThinkPHP, the SESSION-related database driver has been provided. If you download the complete ThinkPHP package, you should see the Extend directory under the ThinkPHP Directory, under this directory, you should see Driver/Session/SessionDb. class. php file. This is the database processing SESSION driver in ThinkPHP.

To enable database processing SESSION in ThinkPHP, you should write the following configuration in the configuration file:

// Custom SESSION processing
'Session _ type' => 'DB ',
Of course, you must write the database configuration.

After the database storage SESSION is enabled, you should create the table stored in the SESSION. The created SQL statement is in SessionDb. class. in the comments of the PHP file, you only need to copy the file and perform some trimming and execute it. The SQL statement is as follows (change the table prefix on your own ):

The code is as follows: Copy code
Create table ly_session (
Session_id varchar (255) not null,
Session_expire int (11) not null,
Session_data blob,
Unique key 'session _ id' ('session _ id ')
);

It is so easy to define the SESSION database driver.

Redis driven by ThinkPHP Custom SESSION

Before writing the Redis driver, we need to know about the implementation of the custom database driver.

Implementation principle of custom SESSION processing in ThinkPHP

Open the Common/function. Php file in the ThinkPHP directory and find the session function. You will see the following code:

The code is as follows: Copy code
/**
* Session management functions
* @ Param string | array $ name if the session name is an array, the session is set.
* @ Param mixed $ value session value
* @ Return mixed
*/
Function session ($ name, $ value = ''){
$ Prefix = C ('session _ prefix ');
If (is_array ($ name) {// session initialization is called before session_start
 
/** Omit 2000 words in some places **/
 
If (C ('session _ type') {// read the SESSION driver
$ Class = 'session '. ucwords (strtolower (C ('session _ type ')));
// Check the driver class
If (require_cache (EXTEND_PATH. 'Driver/Session/'. $ class.'. class. Php ')){
$ Hander = new $ class ();
$ Hander-> execute ();
} Else {
// The class is not defined.
Throw_exception (L ('_ CLASS_NOT_EXIST _'). ':'. $ class );
            }
        }
// Start the session
If (C ('session _ AUTO_START ') session_start ();
} Elseif (''===$ value ){
                
} Else {
                    
        }
} Elseif (is_null ($ value) {// delete a session
       
} Else {// Set the session
       
    }
}

Starting from Line 1 of the code: if the "SESSION_TYPE" configuration item is defined (assuming it is configured as "Redis"), the configuration item is first converted to lowercase, and then the first letter is converted to uppercase, connect to the Session, and load Extend/Driver/Session/SessionRedis under the ThinkPHP directory. class. php file; after loading is successful, it is instantiated and then the execute method is called.

Homepage 1 2 3 Last 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.