This article illustrates the method of PHP implementing session customization. Share to everyone for your reference. The specific analysis is as follows:
Session-defined conversation processor, that is to say, all the operations on the session can be decided by the custom sessions. What does that 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 invoked (the so-called processor, in fact, is a lot of functions/methods). You can set the Session.save_handler to user or memcache, or even a network file system (cloud computing).
Session.save_handler = User: Represents a call to a custom session processor; Session.save_handler = Memcache: Indicates ... Slightly.... (Mostly stored in memory, more efficient).
When setting Session.save_handler = files, the action on the session actually calls the six callback functions of the Session.set_save_handler (specifically looking at the PHP manual) (the so-called callback function, which is the system call, You do not need to specify a call). The six callback functions are shown in the following code, which is also available in the PHP manual:
Copy 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 code above and change it to the way you want it to be called in the Execute PHP file. For example, by default, we save the session file name, which starts with the Sess_, which is actually set here, and you can change it to what you want.
Of course, you can also modify the session save the way, by default is saved in the file, you can save to the database (of course, do not recommend that you do this, the database read more slowly), can also be set to save in memory (the fastest, in the Memcache related content has detailed explanation).
I hope this article will help you with your PHP program design.