[Php] Send another SessionHandle [memcache implementation]-PHP source code

Source: Internet
Author: User
Ec (2); & lt ;? Php & nbsp; ** & nbsp; * & nbsp; session processor & nbsp; * & nbsp; memcache & nbsp; Implementation & nbsp; * & nbsp; @ author & nbsp; MoXie & nbsp; SysTem128@GMail.Com & nbsp; * & nbsp; class & nbsp; SessionHandler & nbs script ec (2); script



/**
* Session Processor
* Memcache implementation
* @ Author MoXie SysTem128@GMail.Com
*/
Class SessionHandler extends Memcache
{
Public $ _ sessionId; # session ID
Public $ _ sessionExpire; # session expiration time
Public $ _ sessionIsQuery;
Public function _ construct ()
{
$ This-> _ sessionExpire = 600;
$ This-> connect ('localhost', 11211 );
}
/**
* Retrieve all positioning Fields
*
*/

/**
* Read Session
*
* @ Return unknown
*/
Public function getSession ($ snKey)
{
$ ReturnInfo = $ this-> get ($ snKey );
$ This-> _ sessionIsQuery = (bool) $ returnInfo;
Return $ returnInfo;
}
/**
* Insert a new Session
*/
Public function insertSession ($ snKey, $ value)
{
Return $ this-> set ($ snKey, $ value, 0, $ this-> _ sessionExpire );
}
/**
* Update session information
*
* @ Return unknown
*/
Public function updateSession ($ snKey, $ value)
{
# Modify statements
Return $ this-> set ($ snKey, $ value, 0, $ this-> _ sessionExpire );
}
/**
* Delete A Session
*
* @ Return unknown
*/
Public function delSession ($ snKey)
{
Return $ this-> delete ($ snKey, 0 );
}
/**
* Delete expired sessions
*
* @ Return unknown
*/
Public function expireSession ()
{
Return true;
}
/**
* Session Initiator
*
*/

Public function on_session_start ()
{
$ This-> _ sessionSite = 1;
$ This-> _ sessionId = session_id ();
Return true;
}
/**
* Session termination
*/
Public function on_session_end ()
{
Return $ this-> expireSession ();
}
/**
* Read Method
*
* @ Param unknown_type $ key
*/
Public function on_session_read ($ key)
{
Return $ this-> getSession ($ key );
}
/**
* Write Method
*
* @ Param unknown_type $ key
* @ Param unknown_type $ value
*/
Public function on_session_write ($ key, $ value)
{
$ This-> getSession ($ key );
If ($ this-> _ sessionIsQuery)
{
$ This-> updateSession ($ key, $ value );
} Else {
$ This-> insertSession ($ key, $ value );
}
Return true;
}
/**
* Destruction method
*
* @ Param unknown_type $ key
*/
Public function on_session_destroy ($ key)
{
Return $ this-> delSession ();
}
/**
* Expiration Method
*
* @ Param integer $ maxLifeTime
*/
Public function on_session_gc ($ maxLifeTime)
{
Return $ this-> expireSession ();
}
}

$ SessionHandler = new SessionHandler ();
Session_set_save_handler (
Array (& $ sessionHandler, ''on _ session_start ''),
Array (& $ sessionHandler, ''on _ session_end ''),
Array (& $ sessionHandler, ''on _ session_read ''),
Array (& $ sessionHandler, ''on _ session_write ''),
Array (& $ sessionHandler, ''on _ session_destroy ''),
Array (& $ sessionHandler, ''on _ session_gc '')
);
Unregister_tick_function (''session _ write_close '');

Session_start ();
// $ _ SESSION [''moxie''] = ''wonderfull! '';
// $ _ SESSION [''system128 ''] = ''wonderfull! '';
// Session_unregister (''moxie '');
Print_r ($ _ SESSION );
?>

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.