The use of the session function in thinkphp

Source: Internet
Author: User
This article is mainly for you to introduce in detail the thinkphp session function, with a certain reference value, interested in small partners can refer to

$_session is used in PHP to operate the session, while Thinkphp provides the session's wrapper function session (). This function alone realizes the function of adding and deleting the session. Let's look at the application and implementation separately.

The definition of the session () function is defined in common/functions.php.

Session Configuration

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

$name = Array (     ' name ' = = ' name ',     ' path ' = '/tmp/',     ' expire ' =>0); session ($NAME);

These are set before the session is opened. When defining the function in thinkphp, it is first to determine whether the $name is an array, if the array is to set the session, and then enter the appropriate code execution settings.

The implementation code is as follows:

if (Is_array ($name)) {//SESSION initialization before Session_Start 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 ($name [' 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 storage System for session provides MySQL and memache two kinds of databases. Of course, file storage is used by default. The code that determines how the session is stored 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 ();//instantiation Processor//Register handler  Session_set_save_handler (     Array ( & $hander, "open"), Array (    & $hander, "Close"), Array (    & $hander, "read"),    Array (& $hander, "Write"),    Array (& $hander, "destroy"),    Array (& $hander, "GC")  );}

The configuration of the session storage system is set by the configuration option Session_type.

Session_type=> ' MySQL '//session is stored in MySQL database

After setting the session automatically start, the system will automatically open the session

Start sessionif (C (' Session_auto_start ')) session_start ();

If you want to turn off session self-booting, set the options Session_auto_start as follows:

Session_auto_start = False

If you turn off system self-booting, you can open the session by manually calling Session_Start () in the project's public file or in the controller. or use the function session (), which is opened as follows:

Session (' [Start] ');

In thinkphp, its implementation code 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

The session assignment is relatively straightforward and is used directly:

Session (' name ', ' ONMPW ');

In addition to the key value can also be multi-layered intermediate use '. ' Connection.

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 ($name 1, $name 2) =  explode ('. ', $name);     if ($prefix) {          $_session[$prefix] [$name 1][$name 2]  = $value;     } else{          $_session[$name 1][$name 2] = $value;     }} else{     if ($prefix) {          $_session[$prefix] [$name]  = $value;     } else{          $_session[$name] = $value;     }}

The $prefix is configured through the option Session_prefix.

Session Fetch Value

The session value is relatively simple.

The first is to get the entire session, using the following method

$values = Session ();

At this point, an array is obtained. The implementation code in thinkphp is as follows:

if (' = = = $name) {  //Get all SESSION  return $prefix? $_session[$prefix]: $_session;}

And then take out a single value

$value 1 = Session (' name ');//or $value2 = Session (' Name1.name2 ');

The implementation code is as follows:

if (Strpos ($name, '. ')) {   list ($name 1, $name 2) =  explode ('. ', $name);   return Isset ($_session[$name 1][$name 2]) $_session[$name 1][$name 2]:null; }else{   return Isset ($_session[$name])? $_session[$name]:null;}

Session Delete

Session deletion is divided into empty session, destroy session and delete single session value.

First, clear the session. Empty the value of the session pass to $name is null

session (NULL); Clear session

The implementation code is as follows:

if (Is_null ($name)) {//empties SESSION   if ($prefix) {    unset ($_session[$prefix]);   } else{    $_session = Array ();}   }

Clearing the session only clears the file or the data in the table, but the file will still exist.

Destroy session

Session (' [Destroy] ');

The implementation code in its thinkphp is as follows:

if (' [destroy] ' = = $name) {//Destroy SESSION   $_session = Array ();   Session_unset ();   Session_destroy ();}

Destroying the session and emptying the session is different, destroying the session will destroy the file together.

The last is to delete a single session value. Use the following methods

Session (' name ', null);

Delete the single session value and set the value of the second parameter $value to NULL to delete.

if (Is_null ($value)) {//delete session  if (Strpos ($name, '. ')) {    list ($name 1, $name 2) =  explode ('. ', $name);    if ($prefix) {      unset ($_session[$prefix] [$name 1][$name 2]);    } else{      unset ($_session[$name 1][$name 2]);    }   } else{    if ($prefix) {      unset ($_session[$prefix] [$name]);    } else{      unset ($_session[$name]);}}  

Check session

Finally, we briefly introduce the examination of the session. The check refers to whether a variable exists. The native PHP check session variable is checked in this way

Isset ($_session[' name ');

Use the session () function after the thinkphp package to check

Session ('? name '); Determine if a session has been set

Its code implementation is also a way of using native checks

$name  = substr ($name, 1); if (Strpos ($name, '. ')) {//Support array   list ($name 1, $name 2) =  explode ('. ', $name);   Return $prefix isset ($_session[$prefix] [$name 1][$name 2]): Isset ($_session[$name 1][$name 2]);} else{   return $prefix isset ($_session[$prefix] [$name]): Isset ($_session[$name]);}

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Related Article

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.