Php custom session example sharing _ PHP Tutorial

Source: Internet
Author: User
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"),...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.