The database stores the session information code and the session code.
Today, I will give you a piece of code,Database stores session informationYou only need to put the following code in the session file, and then introduce sessiong to session_start (). Of course, you don't need to write session_start ().
This is the structure of the database table.
Okay. The following code is available:
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 __, 'deststroy '),
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 ();