PHP implements session-custom sessions with session handler
In this paper, we describe the method of PHP implementing session-Custom sessions processor. Share to everyone for your reference. The specific analysis is as follows:
Session custom Sessions processor, that is to say, all the various operations on the session can be determined by a custom session. What do you mean? First look at the Session.save_handler configuration in php.ini.
By default, Session.save_handler = files, which means that the system-defined processor is called (the so-called processor, which is actually a lot of functions/methods). You can set Session.save_handler to user or memcache, or even to a network file system (cloud computing).
Session.save_handler = User: Represents the invocation of a custom session processor; Session.save_handler = Memcache: Indicates ... Slightly.... (roughly saved in memory, more efficient).
When setting Session.save_handler = files, about the session operation, actually called the Session.set_save_handler (Specific view PHP manual) six callback function (so-called callback function, is called by the system, You do not need to specify a call). Six callback functions are shown in the following code, as well as in the PHP manual:
Copy the Code code as follows: <?php
function open ($save _path, $session _name)
{
Global $sess _save_path;
$sess _save_path = $save _path;
return (true);
}
function Close ()
{
return (true);
}
function Read ($id)
{
Global $sess _save_path;
$sess _file = "$sess _save_path/sess_$id";
Return (String) @file_get_contents ($sess _file);
}
function Write ($id, $sess _data)
{
Global $sess _save_path;
$sess _file = "$sess _save_path/sess_$id";
if ($fp = @fopen ($sess _file, "W")) {
$return = fwrite ($fp, $sess _data);
Fclose ($FP);
return $return;
} else {
return (false);
}
}
function Destroy ($ID)
{
Global $sess _save_path;
$sess _file = "$sess _save_path/sess_$id";
Return (@unlink ($sess _file));
}
Function GC ($MAXLIFETIME)
{
Global $sess _save_path;
foreach (Glob ("$sess _save_path/sess_*") as $filename) {
if (Filemtime ($filename) + $maxlifetime < time ()) {
@unlink ($filename);
}
}
return true;
}
Session_set_save_handler ("Open", "close", "read", "write", "Destroy", "GC");
Session_Start ();
Proceed to use sessions normally
?>
When you set the Session.save_handler to user, you can rewrite the above code, change it to the method you need, and call it in executing PHP. For example, by default, we save the file name of the session, all start with Sess_, is actually set here, you can change to what you want.
Of course, you can also modify the way the session is saved, by default is saved in the file, you can save to the database (of course, it is not recommended that you do so, the database read slower), can also be set to save in memory (the fastest, in the Memcache related content in detail).
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/948405.html www.bkjia.com true http://www.bkjia.com/PHPjc/948405.html techarticle PHP Implementation of the session to customize the sessions of the method, session sessions in this article on the implementation of the session of PHP, the method of custom sessions processor. Share to everyone for your reference. Specific analysis ...