Initializes the session class: This−>load−>library (' session′); After initialization, you can use This->session to access the session object.
In CLI mode, the session class is automatically closed.
In previous versions of CodeIgniter, the session class did not implement a lock mechanism, which meant that two HTTP requests might use the same sessions at the same time. The more professional point is that the request is non-blocking. (Requests were non-blocking)
Using non-blocking requests while working on sessions also means unsafe, because modifying the session data (or regenerating it) in one request can affect the concurrent second request. This is the cause of many problems and is why CodeIgniter 3.0 completely rewrites the session class.
The session class is available with 4 different drivers (or storage engines):
Files/database/redis/memcached. By default, file drivers are used when the session is initialized
Class diagram for session implementation
One, interface: sessionhandlerinterface.php defines an interface that contains 6 methods
Customize session driver See the article "user-defined session processing mechanism in PHP"
Interface Sessionhandlerinterface {public
function open ($save _path, $name);
Public function Close ();
Public function read ($session _id);
Public Function Write ($session _id, $session _data);
Public function Destroy ($session _id);
Public Function GC ($MAXLIFETIME);
Second, extract class: Ci_session_driver
Extract Class Ci_session_driver defines the specification for the return value (Success/failure) of the session five interfaces (except the Read method is returned data)
and defines the lock/unlock method. The lock
is used in the Ci_session class that follows.
Abstract class Ci_session_driver implements Sessionhandlerinterface {protected $_config;
Protected $_fingerprint;//The MD5 summary that holds the contents of the session file protected $_lock = FALSE;
protected $_session_id;
Defines a value that returns success or failure in the Sessionhandlerinterface interface method protected $_success, $_failure;
Public function __construct (& $params) {$this->_config =& $params;
interface method, all functions need to return success or failure. PHP7 Previous versions were successful with 0,-1 indicates failure.
The PHP7 changed to true to represent success, and false represents failure.
If you don't pay attention to the distinction, then 0 values in different versions of the meaning of the opposite.
So in the constructor, according to the current PHP version, the attribute $this->_success, $this->_failure is defined.
if (is_php (' 7 ')) {$this->_success = TRUE;
$this->_failure = FALSE;
else {$this->_success = 0;
$this->_failure =-1;
}//When the Destory () method is invoked, the cookie protected function _cookie_destroy () {return Setcookie () of the client is emptied (
$this->_config[' Cookie_name '], NULL, 1, $this->_config[' Cookie_path '], $this->_config[' Cookie_domain '], $this
->_config[' Cookie_secure '], TRUE);
//session lock mechanism, to the current request in the session pair like locked protected function _get_lock ($session _id) {$this->_lock = TRUE;
return TRUE;
//session lock mechanism to unlock protected function _release_lock () {if ($this->_lock) {in the current request.
$this->_lock = FALSE;
return TRUE; }
}
Class of the session, exposed to the user to call classes
Config configuration
The session Common configuration reference PHP. INI Configuration: Session Configuration Detailed description "
$config [' sess_driver '] = ' files '//drive type
$config [' sess_cookie_name '] = ' ci_session ';//client store SessionID Cookies key value
$config [' sess_expiration '] = 7200;//session Expiration Time
$config [' sess_save_path '] = NULL; The storage path
$config [' sess_match_ip '] = FALSE;
$config [' sess_time_to_update '] =;
$config [' sess_regenerate_destroy '] = FALSE;
Get Session data
1, name= _session[' name '];
2, Name= this->session->name;
Realized by __get Magic method
Public Function __get ($key)
{
//If the requested key exists, returns if
(Isset ($_session[$key)) {return
$_ session[$key];
}
If it is $this->session->session_id, return session_id ()
elseif ($key = = ' session_id '
)
session_id ();
}
return NULL;
}
3, Name= this->session->userdata (' name ');//If the access item does not exist, return NULL
Get all existing: $this->session->userdata ();
Public Function UserData ($key = NULL)
{
//if $key exists and hit, returns a value, or returns NULL if no hit (
isset ($key))
{
return Isset ($_session[$key])? $_session[$key]: NULL;
}
If the key is null and $_session has no data, an empty array
elseif (Empty ($_session))
{return
array ()
}
is returned. The following starts processing $key null case, the function returns all $_session case
$userdata = Array ();
The key that is used for two special data to be excluded in the return value is a flash_key that fails at once, one that expires on Temp_key
//Because they are all stored in the __ci_vars key, so __ci_ VARs itself this key also excludes
$_exclude = array_merge (
array (' __ci_vars '),
$this->get_flash_keys (),
$ This->get_temp_keys ()
);
Traverse $_session and generate $userdata
foreach (Array_keys ($_session) as $key)
{
if (! In_array ($key, $_exclude, TRUE)
{
$userdata [$key] = $_session[$key];
}
}
return $userdata;
}
Add Session Data
$newdata = Array (
' username ' => ' johndoe ', '
email ' => ' johndoe@some-site.com ', '
logged_ In ' => TRUE
);
$this->session->set_userdata ($newdata);
$this->session->set_userdata (' some_name ', ' some_value ');
The Set_userdata function is as follows: public
function Set_userdata ($data, $value = NULL)
{
//parse array mode
if (Is_array $ Data)
{
foreach ($data as $key => & $value)
{
$_session[$key] = $value;
}
return;
}
Parsing key-value pairs
$_session[$data] = $value;
}
Delete session data
unset ($_session[' some_name '));
or multiple values:
unset (
$_session[' some_name '),
$_session[' another_name ']
);
or
$this->session->unset_userdata (' some_name ');
Unset_userdata implement public
function Unset_userdata ($key)
{
//array mode
if (Is_array ($key))
{
foreach ($key as $k)
{
unset ($_session[$k]);
}
return;
}
Single key
unset ($_session[$key]);
Destroy session
Session_destroy ();
or
$this->session->sess_destroy ();
Sess_destroy is to invoke the native Session_destroy () method public
function Sess_destroy ()
{
Session_destroy ();
}
When the session has been processed and no longer needed, you also keep the session open and it is easy to cause inefficiencies. Therefore, you must close the session that is no longer needed when the current request is closed. Simply put: When you no longer need to use a session variable, use the Session_write_close () method to close it.
Register_shutdown_function (' Session_write_close ');