This example describes the PHP encapsulated database save session function class. Share to everyone for your reference, specific as follows:
PHP saves the session class with the database:
<?php class Safesessionhandler implements Sessionhandlerinterface {public $save _path;
Public $session _name;
Public $table;
Public Function __construct () {$this->table = new Table ("Safe_session");
Private Function Session_id_parse ($session _id) {$time = Hexdec (substr ($session _id, 0, 8));
$skey = substr ($session _id, 8);
Return Array ($time, $skey);
Public function Close () {Loginfo (' close: ');
return true;
The Public Function Create_sid () {Loginfo ("Create_sid:");
$time = time ();
$skey = "";
$char = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
For ($i =0 $i <52; $i + +) {$skey. = $char {Mt_rand (0, 61)};
$session = Array ("Time" => $time, "Skey" => $skey, "Sval" => "",);
$this->table->insert ($session); Return Dechex ($time).
$skey;
The Public function destroy ($session _id) {loginfo ("Destroy:%s", $session _id); List ($time, $skey) = $this->session_id_parse ($session _id);
$this->table->where ("time =?", $time)->where ("Skey =?", $skey)->delete ();
return true;
The Public Function gc ($maxlifetime) {loginfo ("GC:%s", $maxlifetime);
$this->table->where ("Time <.", Time ()-86400 *)->delete ();
return true;
Public function open ($save _path, $session _name) {loginfo ("Open:%s,%s", $save _path, $session _name);
$this->save_path = $save _path;
$this->session_name = $session _name;
return true;
The public function read ($session _id) {loginfo ("read:%s", $session _id);
List ($time, $skey) = $this->session_id_parse ($session _id);
$row = $this->table->where ("time =?", $time)->where ("Skey =?", $skey)->select ()->fetch ();
if (empty ($row)) {return "";
return $row ["Sval"];
Public Function Write ($session _id, $session _data) {loginfo ("write:%s,%s", $session _id, $session _data); $session = Array ("Sval" => $session _data);
List ($time, $skey) = $this->session_id_parse ($session _id);
$this->table->where ("time =?", $time)->where ("Skey =?", $skey)->update ($session);
return true;
}
}
For more information about PHP interested readers can view the site topics: "PHP string (String) Usage summary", "PHP array Operation techniques Encyclopedia", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", "PHP basic grammar Introductory Course" , "PHP Operation Office Document Skills Summary (including word,excel,access,ppt)", "PHP date and Time usage summary", "PHP object-oriented Programming Introductory Course", "Php+mysql database operation Introductory Course" and " A summary of common PHP database operations tips
I hope this article will help you with the PHP program design.