Database store session Information code, store session code
Today to everyone on a piece of code, the database stores session information , you just need to put the following code in the session file, and then Session_Start () to introduce the Sessiong file on the line, Of course you don't have to write Session_Start ().
This is the structure of the database table
Okay, here's the code.
Class Session {
private static $_mysqli;
public static function action () {
Ini_set (' Session.save_handler ', ' user ');
Session_set_save_handler (Array (__class__, ' open '),
Array (__class__, ' close '),
Array (__class__, ' read '),
Array (__class__, ' write '),
Array (__class__, ' destroy '),
Array (__class__, ' GC '));
@session_start ();
}
public static function open ($path, $name) {
Self::$_mysqli = new mysqli (' localhost ', ' root ', ' ', ' test ');
return true;
}
public static function close () {
Self::$_mysqli->close ();
return true;
}
public static function read ($SID) {
$_query = "Select Sdata from session WHERE sid= ' {$sid} ' LIMIT 1";
$_result = Self::$_mysqli->query ($_query);
$_sdata = $_result->fetch_object ();
Return $_sdata->sdata;
}
public static function Write ($sid, $sdata) {
$_query = "Select Sid from Session WHERE sid= ' {$sid} ' LIMIT 1";
$_result = Self::$_mysqli->query ($_query);
if (!! $_sid = $_result->fetch_object ()) {
$_query = "UPDATE session SET sdata= ' {$sdata} ' WHERE sid= ' {$sid} '";
Self::$_mysqli->query ($_query);
} else {
$_query = "INSERT into session (Sid,sdata) VALUES (' {$sid} ', ' {$sdata} ')";
Self::$_mysqli->query ($_query);
}
return true;
}
public static function Destroy ($SID) {
$_query = "DELETE from session WHERE sid= ' {$sid} ' LIMIT 1";
Self::$_mysqli->query ($_query);
Setcookie (Ini_get (' Session.name '), ", Time ()-1);
return true;
}
public static function GC ($maxlifetime) {
$_query = "DELETE from session WHERE now ()-slasttime> ' {$maxlifetime} '";
Self::$_mysqli->query ($_query);
return true;
}
}
Session::action ();
http://www.bkjia.com/PHPjc/1014828.html www.bkjia.com true http://www.bkjia.com/PHPjc/1014828.html techarticle Database storage Session information code, stored session code today to everyone on a piece of code, database storage session information, you just need to put the following code in the session file, then ...