Php custom session example sharing

Source: Internet
Author: User
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 ";

,

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.