This example describes a class in which PHP stores session information to a database. Share to everyone for your reference. The specific analysis is as follows:
Sessionhandlerinterface interface is a built-in PHP interface, directly implemented on the line
You can see the PHP manual on the Session_set_save_handler function explanation!
The PHP code is as follows:
Copy Code code as follows:
/**
* class that the session information is stored in the database
* Table structure:
* CREATE TABLE IF not EXISTS ' Sessioninfo ' (
* ' SID ' varchar (255) Not NULL,
* ' value ' text not NULL,
* ' Expiration ' timestamp not NULL DEFAULT current_timestamp on UPDATE current_timestamp,
* PRIMARY KEY (' Sid ')
*) Engine=innodb DEFAULT Charset=utf8;
*/
Class Mysessionhandler implements Sessionhandlerinterface {
/**
* @access Private
* @var Object Database connection
*/
Private $_dblink;
/**
* @access Private
* @var string to save the table name for the session
*/
Private $_sessiontable;
/**
* @access Private
* @var String Session Name
*/
Private $_sessionname;
/**
* @const Expiration Time
*/
Const Session_expire = 10;
Public function __construct ($dbLink, $sessionTable) {
if (!is_object ($dbLink)) {
return false;
}
$this->_dblink = $dbLink;
$this->_sessiontable = $sessionTable;
}
/**
* Open
* @access Public
* @param string $session _save_path Save the path to the session
* @param string $session _name session Name
* @return Integer
*/
Public function open ($session _save_path, $session _name) {
$this->_sessionname = $session _name;
return 0;
}
/**
* Close
* @access Public
* @return Integer
*/
Public function Close () {
return 0;
}
/**
* Close Session
* @access Public
* @param string $session _id session ID
* @return String
*/
Public function read ($session _id) {
$query = "Select value from {$this->_sessiontable} WHERE sid = {$session _id} and Unix_timestamp (expiration) +". Self::session_expire. "> Unix_timestamp (Now ())";
$result = $this->_dblink->query ($query);
if (!isset ($value) | | empty ($value)) {
$value = "";
return $value;
}
$this->_dblink->query ("UPDATE {$this->_sessiontable} SET expiration = Current_timestamp () WHERE sid = {$ SESSION_ID} ");
$value = $result->fetch_array ();
$result->free ();
return $value [' value '];
}
/**
* Write session
* @access Public
* @param string $session _id session ID
* @param string $session _data session data
* @return Integer
*/
Public Function Write ($session _id, $session _data) {
$query = "Select value from {$this->_sessiontable} WHERE sid = ' {$session _id} ' and Unix_timestamp (expiration) +". Self::session_expire. "> Unix_timestamp (Now ())";
$result = $this->_dblink->query ($query);
$result = $result->fetch_array ();
if (!empty ($result)) {
$result = $this->_dblink->query ("UPDATE {$this->_sessiontable} SET value = {$session _data} WHERE sid = {$session _ID} ");
}
else{
$result = $this->_dblink->query (INSERT into {$this->_sessiontable} (SID, Value) VALUES (' {$session _id} ', ' {$ Session_data} '));
}
if ($result) {
return 0;
}
else{
return 1;
}
}
/**
* Ecstasy session
* @access Public
* @param string $session _id session ID
* @return Integer
*/
Public function Destroy ($session _id) {
$result = $this->_dblink->query ("DELETE from {$this->_sessiontable} WHERE sid = ' {$session _id} '");
if ($result) {
return 0;
}
else{
return 1;
}
}
/**
* Garbage Collection
* @access Public
* @param string $maxlifetime The maximum lifetime of the session
* @return Integer
*/
Public Function GC ($MAXLIFETIME) {
$result = $this->_dblink->query ("DELETE from {$this->_sessiontable} WHERE unix_timestamp (expiration) < Unix_timestamp (now ())-". Self::session_expire);
if ($result) {
return 0;
}
else{
return 1;
}
}
}
$dbLink = new Mysqli ("localhost", "root", "root", "test");
$sessionTable = "Sessioninfo";
$handler = new Mysessionhandler ($dbLink, $sessionTable);
Session_set_save_handler ($handler);
Session_Start ();
$_session[' name ' = ' Test ';
echo $_session["name"];
Session_destroy ();
I hope this article will help you with your PHP program design.