$_session is used in PHP to manipulate sessions, while Thinkphp provides a session-wrapping function session (). This function alone realizes the function of adding and deleting the session and changing the check. Below we look at its application and implementation.
The definition of the session () function is defined in common/functions.php.
Session Configuration
The session ($name = ', $value = ') function has two parameters that are set for the session $name the array. Use the following:
$name = Array (
' name ' => ' name ', '
path ' => '/tmp/', '
expire ' =>0
);
Session ($NAME);
These are set before the session is opened. When you define the function in thinkphp, you first determine whether $name is an array, and if it is an array, you are setting up the session and then entering the appropriate code execution settings.
The implementation code is as follows:
if (Is_array ($name)) {//session Session_Start invoke the IF (Isset ($name [' prefix ')]) C (' Session_prefix ', $name [' prefix ']
); if (C (' var_session_id ') && isset ($_request[c (' var_session_id ')]) {session_id (' $_request[c ')
)]);
}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, there are two databases of MySQL and Memache for the session storage system. Of course, the file store is used by default. The code that determines how the session is stored is as follows:
if (c (' Session_type ')) {//Read session driven
$type = C (' Session_type ');
The system calls the MySQL driver
$class = Strpos ($type, ' \ \ ")? $type: ' think\\session\\driver\\ '. Ucwords (Strtolower ($type));
$hander = new $class ();////Instantiate
Processor//Register
Session_set_save_handler (
Array (& $hander, "open"),
Array (& $hander, "Close"),
Array (& $hander, "read"),
Array (& $hander, "write"
), Array (& $hander, "destroy"),
Array (& $hander, "GC")
);
The configuration for the session storage system is set through the configuration options session_type.
Session_type=> ' MySQL '//store session in MySQL database
If you set the session to start automatically when the setting is complete, the system will automatically open sessions
Start session
if (C (' Session_auto_start ')) session_start ();
If you want to turn off session from start, set the options Session_auto_start as follows:
Session_auto_start => False
If system startup is turned off, you can open the session in the project's public file or in the controller by manually calling Session_Start (). or use the function session (), which opens the following method:
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, direct use:
Session (' name ', ' ONMPW ');
In addition to the value of the key can also be multiple layers of intermediate use '. ' Connection.
Session (' Name1.name2 ', ' ONMPW '); Equivalent to $_session[' name1 ' [' name2 '] = ' ONMPW ';
The implementation code for the 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 option Session_prefix.
Session Take value
The session value is relatively simple.
The first is to get all the sessions, using the following methods
$values = Session ();
At this point, we get an array. 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
$value 2 = 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 deletion
The deletion of session is divided into empty session, destroy session and delete single session value.
First, empty session. Empty session pass to $name value is null
session (NULL); Empty session
The implementation code is as follows:
if (Is_null ($name)) {//empty session
if ($prefix) {
unset ($_session[$prefix]);
} else{
$_session = Array ();
}
}
Emptying the session only clears the data in the corresponding file or table, but the file still exists.
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 it is different from destroying sessions to destroy the files together.
The last is to delete a single session value. Use the following method
Session (' name ', null);
Deletes a single session value and sets the value of the second parameter $value to null.
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, the review of the session is briefly introduced. The check refers to whether a variable exists. Native PHP Check session variable is so checked
Isset ($_session[' name '));
The session () function is used after the thinkphp encapsulation to check
Session ('? name '); Determine if a session has been set
The code implementation is also taking advantage of the way the 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]);
This is almost an introduction to the functions of the session () function, and how thinkphp is implemented. I hope the content of this article will help you in the process of using thinkphp.