This article mainly introduces php custom session examples. For more information, see
This article mainly introduces php custom session examples. For more information, see
The following is the session 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 ";
,