This article mainly introduces the example of php writing session into the database. For more information, see
The Code is as follows:
Class session_handler {
Protected $ maxlifetime = null;
Protected $ dbHandle = null;
Public $ config = null;
Public static function init ($ args ){
Return new self ($ args );
}
Public function _ construct ($ args ){
$ This-> config = $ args;
$ This-> maxlifetime = get_cmd_var ("session. gc_maxlifetime ");
Session_set_save_handler (
Array ($ this, "open "),
Array ($ this, "close "),
Array ($ this, "read "),
Array ($ this, "write "),
Array ($ this, "destroy "),
Array ($ this, "gc "));
}
Public function open (){
$ This-> link = mysqli_connect (
$ This-> config ['host'],
$ This-> config ['user'],
$ This-> config ['Password'],
$ This-> config ['database']);
Mysqli_set_charset ($ this-> link, "utf8 ");
$ SQL = 'create TABLE IF NOT EXISTS '% s '(
'Session _ id' varchar (255) not null,
'Session _ data' text,
'Session _ expires' char (10) not null,
Primary key ('session _ id ')
) ENGINE = InnoDB default charset = utf8 COLLATE = utf8_unicode_ci ;';
$ SQL = sprintf ($ SQL, $ this-> config ['table']);
Mysqli_query ($ this-> link, $ SQL );
Return true;
}
Public function close (){
Return true;
}
Public function read ($ session_id ){
If (empty ($ session_id) return null;
$ SQL = 'select' session _ data' AS 'data' FROM '% s' WHERE 'session _ id' = "% s" AND 'session _ expires'> "% u "';
$ SQL = sprintf ($ SQL,
Mysqli_real_escape_string ($ this-> link, $ this-> config ['table']),
Mysqli_real_escape_string ($ this-> link, $ session_id ),
Time ());
$ Result = mysqli_query ($ this-> link, $ SQL );
$ Row = mysqli_fetch_assoc ($ result );
Return $ row ['data'];
}
Public function write ($ session_id, $ session_data ){
If (empty ($ session_id) return null;
$ NewExpires = time () + $ this-> maxlifetime;
$ SQL = 'replace INTO '% s' SET 'session _ id' = "% s", 'session _ data' = "% s ", 'session _ expires' = "% u "';
$ SQL = sprintf ($ SQL,
Mysqli_real_escape_string ($ this-> link, $ this-> config ['table']),
Mysqli_real_escape_string ($ this-> link, $ session_id ),
Mysqli_real_escape_string ($ this-> link, $ session_data ),
$ NewExpires );
$ Result = mysqli_query ($ this-> link, $ SQL );
Return mysqli_affected_rows ($ this-> link );
}
Public function destroy ($ session_id ){
$ SQL = 'delete FROM '% s' WHERE 'session _ id' = "% s "';
$ SQL = sprintf ($ SQL,
Mysqli_real_escape_string ($ this-> link, $ this-> config ['table']),
Mysqli_real_escape_string ($ this-> link, $ session_id ));
$ Result = mysqli_query ($ this-> link, $ SQL );
Return mysqli_affected_rows ($ this-> link );
}
Public function gc (){
$ SQL = 'delete FROM '% s' WHERE 'session _ exists' <"% u "';
$ SQL = sprintf ($ SQL,
Mysqli_real_escape_string ($ this-> link, $ this-> config ['table']),
Time ());
$ Result = mysqli_query ($ this-> link, $ SQL );
Return mysqli_affected_rows ($ this-> link );
}
}
Class session {
Public static $ collection = null;
Public static function open ($ clean = false, $ token = false ){
If ($ clean) ob_end_clean ();
If ($ token) session_id ($ token );
Session_start ();
Self: $ collection =$ _ SESSION;
}
Public static function id (){
$ Num_args = func_num_args ();
If ($ num_args ){
$ Args = func_get_arg (0 );
Return session_id ($ args );
} Else {
Return session_id ();
}
}
Public static function get ($ name ){
Return isset ($ _ SESSION [$ name])? $ _ SESSION [$ name]: null;
}
Public static function set ($ name, $ value ){
$ _ SESSION [$ name] = $ value;
Return true;
}
Public static function delete ($ name ){
If (! Isset ($ _ SESSION [$ name]) return null;
Unset ($ _ SESSION [$ name]);
Return true;
}
Public static function destroy (){
Session_destroy ();
}
}
$ Config = array (
"Host" => "127.0.0.1 ",
"User" => "root ",
"Password" => "123456 ",
"Database" => "test ",
"Charset" => "utf8 ",
"Table" => "user_session ");
Session_handler: init ($ config );
Session: open ();
Session: set ("profile", array ("id" => 1, "user" => "haowei", "vip-level" => 6 ));