Session functions and ThinkPHP functions in thinkphp

Source: Internet
Author: User

Session functions and ThinkPHP functions in thinkphp

In PHP, $ _ SESSION is used to operate sessions. ThinkPHP provides the session encapsulation function session (). This function is used to add, delete, modify, and query sessions. Next we will look at its application and implementation.

The session () function is defined in Common/functions. php.

Session Configuration

The session ($ name = '', $ value ='') function has two parameters. When $ name is an array, the session is set. Use:

$name = array(     ‘name'=>'name',     ‘path'=>'/tmp/',     ‘expire'=>0);session($name);

These settings are made before the session is enabled. When defining this function in ThinkPHP, you first determine whether $ name is an array. If it is an array, it indicates that you are setting the session and then entering the corresponding code execution settings.

The implementation code is as follows:

If (is_array ($ name) {// before session Initialization, call if (isset ($ name ['prefix']) C ('session _ prefix ', $ name ['prefix']); if (C ('var _ SESSION_ID ') & isset ($ _ REQUEST [C ('var _ SESSION_ID')]) {session_id ($ _ REQUEST [C ('var _ SESSION_ID ')]);} elseif (isset ($ name ['id']) {session_id ($ name ['id']);} if ('common '! = APP_MODE) {// other modes may not support ini_set ('session. auto_start ', 0);} if (isset ($ name ['name']) session_name ($ name ['name']); if (isset ($ name ['path']) session_save_path ($ name ['path']); if (isset ($ name ['domain']) ini_set ('session. cookie_domain ', $ name ['domain']); if (isset ($ name ['expire ']) {ini_set ('session. gc_maxlifetime', $ name ['expire ']); ini_set ('session. cookie_lifetime ', $ name ['expire']);} if (isset ($ na Me ['use _ trans_sid ']) ini_set ('session. use_trans_sid', $ name ['use _ trans_sid ']? 1:0); if (isset ($ name ['use _ cookies ']) ini_set ('session. use_cookies', $ name ['use _ cookies ']? 1-0); if (isset ($ name ['cache _ limiter ']) session_cache_limiter ($ name ['cache _ limiter']); if (isset ($ name ['cache _ expire ']) session_cache_expire ($ name ['cache _ expire']); if (isset ($ name ['type']) C ('session _ type', $ name ['type']);……}

In ThinkPHP, the session storage system provides two types of databases: mysql and memache. Of course, file storage is used by default. The code used to determine the session storage method is as follows:

If (C ('session _ type') {// read SESSION driver $ TYPE = C ('session _ type '); // The system calls the mysql driver $ class = strpos ($ type ,'\\')? $ Type: 'Think \ Session \ Driver \\'. ucwords (strtolower ($ type); $ hander = new $ class (); // instantiate the processor // register the processor session_set_save_handler (array (& $ hander, "open "), array (& $ hander, "close"), array (& $ hander, "read"), array (& $ hander, "write"), array (& $ hander, "destroy"), array (& $ hander, "gc "));}

The session storage system is configured by configuring the SESSION_TYPE option.

SESSION_TYPE => 'mysql' // store the session in the Mysql database

After the setting is complete, if the session is set to automatically start, the system automatically starts the session.

// Start sessionif (C ('session _ AUTO_START ') session_start ();

To disable session auto-start, set SESSION_AUTO_START as follows:

SESSION_AUTO_START => false

If the system auto-start is disabled, you can enable the session in the public file of the project or by manually calling session_start () in the controller. You can also use the function session () to enable the function as follows:

Session ('[start]');

The implementation code in ThinkPHP is as follows:

If ('[pause]' ==$ name) {// pause session session_write_close ();} elseif ('[start]' = $ name) {// start session session_start ();} elseif ('[destroy]' ==$ name) {// destroy session $ _ SESSION = array (); session_unset (); session_destroy ();} elseif ('[regenerate]' ==$ name) {// regenerate id session_regenerate_id ();}

Session assignment

Session assignment is relatively simple and can be used directly:

Session ('name', 'onmpw ');

In addition, the key value can be connected by '.' In the middle of multiple layers.

Session ('name1. name2', 'onmpw'); // equivalent to $ _ SESSION ['name1'] ['name2'] = 'onmpw ';

The implementation code for session assignment in ThinkPHP is as follows:

if(strpos($name,'.')){     list($name1,$name2) =  explode('.',$name);     if($prefix){          $_SESSION[$prefix][$name1][$name2]  = $value;     }else{          $_SESSION[$name1][$name2] = $value;     }}else{     if($prefix){          $_SESSION[$prefix][$name]  = $value;     }else{          $_SESSION[$name] = $value;     }}

$ Prefix is configured by selecting SESSION_PREFIX.

Session Value

The session value is relatively simple.

The first step is to obtain all sessions. The usage is as follows:

$ Values = session ();

In this case, an array is obtained. The implementation code in ThinkPHP is as follows:

If (''= $ name) {// get all session return $ prefix? $ _ SESSION [$ prefix]: $ _ SESSION ;}

Then retrieve a single value.

$ Value1 = session ('name'); // or $ value2 = session ('name1. name2 ');

The implementation code is as follows:

if(strpos($name,'.')){   list($name1,$name2) =  explode('.',$name);   return isset($_SESSION[$name1][$name2])?$_SESSION[$name1][$name2]:null; }else{   return isset($_SESSION[$name])?$_SESSION[$name]:null;}

Delete session

The deletion of a session can be divided into clearing the session, destroying the session, and deleting a single session value.

Clear the session first. The value of parameter $ name passed to the clear session is null.

Session (null); // clear the session

The implementation code is as follows:

If (is_null ($ name) {// clear session if ($ prefix) {unset ($ _ SESSION [$ prefix]);} else {$ _ SESSION = array ();}}

Clearing the session only clears the data in the file or table corresponding to the session, but the file still exists.

Destroy session

Session ('[destroy]');

The implementation code in ThinkPHP is as follows:

If ('[destroy]' ==$ name) {// destroy session $ _ SESSION = array (); session_unset (); session_destroy ();}

The difference between destroying a session and clearing a session is that destroying a session will destroy the file together.

The last step is to delete a single session value. The usage is as follows:

Session ('name', null );

Delete a single session value and set the value of the second $ value to null.

If (is_null ($ value) {// Delete session if (strpos ($ name ,'. ') {list ($ name1, $ name2) = explode ('. ', $ name); if ($ prefix) {unset ($ _ SESSION [$ prefix] [$ name1] [$ name2]);} else {unset ($ _ SESSION [$ name1] [$ name2]) ;}} else {if ($ prefix) {unset ($ _ SESSION [$ prefix] [$ name]);} else {unset ($ _ SESSION [$ name]) ;}}

Check session

Finally, we will briefly introduce the session check. Check indicates whether a variable exists. The native PHP checks session variables in this way.

Isset ($ _ SESSION ['name']);

The session () function is used after ThinkPHP encapsulation.

Session ('? Name'); // determines whether a session has been set

Its code implementation also utilizes the native check method.

$ Name = substr ($ name, 1); if (strpos ($ name ,'. ') {// supports array list ($ name1, $ name2) = explode ('. ', $ name); return $ prefix? Isset ($ _ SESSION [$ prefix] [$ name1] [$ name2]): isset ($ _ SESSION [$ name1] [$ name2]);} else {return $ prefix? Isset ($ _ SESSION [$ prefix] [$ name]): isset ($ _ SESSION [$ name]);}

The above section describes the use of various functions of the session () function and how ThinkPHP is implemented. I hope this article will help you in using ThinkPHP.

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.