Php custom session example sharing. The following code copies the session class code :? Phpclasssession {staticfunctioninit () {session_set_save_handler (array (session, open), array (session, close), and below a is the session class code
The code is as follows:
Class session
{
Static function init ()
{
Session_set_save_handler (
Array ("session", "open "),
Array ("session", "close "),
Array ("session", "read "),
Array ("session", "write "),
Array ("session", "destroy "),
Array ("session", "gc ")
);
}
Static function open ($ save_path, $ session_name)
{
Echo "session opening!
";
/* Global $ db, $ REMOTE_ADDR;
$ Rs = $ db-> Execute ("select * from Sessions where SessionID = '". session_id ()."'");
$ Arry = $ rs-> FetchRow ();
If ($ rs & $ arry)
{
$ Db-> Execute ("update Sessions set SessionLast = NOW () where SessionID = '". session_id ()."'");
}
Else
{
$ Query = "insert into Sessions set SessionID = '". session_id (). "', SessionName = '$ REMOTE_ADDR', SessionLast = 'Now ()'";
// Echo $ query;
$ Db-> Execute ($ query );
}*/
Return true;
}
Static function close ()
{
Return (true );
}
Static function read ($ id)
{
Echo "session reading now!
";
Global $ db;
Return true;
$ Timenow = strftime ("% Y-% m-% d % H: % M: % S", time ());
$ Query = "select SessionData from Sessions where SessionID = '$ ID' and SessionLast>' $ timenow '";
$ Rs = $ db-> Execute ($ query );
If (list ($ SessionData) = $ rs-> FetchRow ())
{
// Echo $ SessionData;
Return $ SessionData;
}
Else
{
Return false;
}
}
Static function write ($ id, $ sess_data)
{
Echo "session writing now!
";
Global $ db;
$ Rs = $ db-> Execute ("select SessionID from Sessions where SessionID = '$ ID '");
$ Num = $ rs-> RecordCount ();
$ Unix_time = time () + MY_SESS_TIME;
// Echo MY_SESS_TIME;
$ Dateleft = strftime ("% Y-% m-% d % H: % M: % S", $ unix_time );
If ($ num <= 0)
{
$ SQL = "insert into Sessions set SessionData = '$ sess_data', SessionName = '". $ _ SERVER ["REMOTE_ADDR"]. "', SessionLast =' $ dateleft ', SessionID = '". session_id (). "'";
}
Else
{
$ SQL = "update Sessions set SessionData = '$ sess_data', SessionName = '". $ _ SERVER ["REMOTE_ADDR"]. "', SessionLast =' $ dateleft 'where SessionID =' $ ID '";
}
$ Db-> Execute ($ SQL );
}
Static function destroy ($ id)
{
Echo "session destroying now!
";
Global $ db;
$ SQL = "DELETE FROM Sessions WHERE 'sessionid' = '$ ID '";
$ Rs = $ db-> Execute ($ SQL );
Return $ rs;
// $ Sess_file = "$ sess_save_path/sess _ $ id ";
// Return (@ unlink ($ sess_file ));
}
/*************************************** ******
* WARNING-You will need to implement some *
* Sort of garbage collection routine here .*
**************************************** *****/
Static function gc ($ maxlifetime)
{
Echo "session maxlifetime now!
";
Global $ db;
$ Timenow = strftime ("% Y-% m-% d % H: % M: % S", time ());
$ SQL = "DELETE FROM '$ table_session' WHERE 'sessionla' <' $ timenow '";
Return $ sess_db-> Execute ($ SQL );
// Echo "now gc!
";
Return true;
}
// Proceed to use sessions normally
}
Usage
The code is as follows:
Include ("session. class. php ");
Session: init ();
Session_start ();
Define ("MY_SESS_TIME", 3600); // SESSION survival duration
$ _ SESSION ["test"] = "abcdef ";
The pipeline code is as follows :? Php class session {static function init () {session_set_save_handler (array ("session", "open"), array ("session", "close"),...