PHP writes sessions to memcache.

Source: Internet
Author: User
<? PHP class memsession {Private Static $ handler = NULL; Private Static $ lifetime = NULL; Private Static $ time = NULL; const NS = 'session _'; private Static function Init ($ handler) {self ::$ handler = $ handler; self ::$ lifetime = ini_get ('session. gc_maxlifetime '); self: $ time = Time ();} public static function start (memcache $ memcache) {self: Init ($ memcache ); session_set_save_handler (Array (_ class __, "open"), array (_ Class __, "close"), array (_ class __, "read"), array (_ class __, "write "), array (_ class __, "Destroy"), array (_ class __, "GC"); session_start ();} public static function open ($ path, $ name) {return true;} public static function close () {return true;} public static function read ($ PHPSESSID) {$ out = self :: $ Handler-> get (SELF: session_key ($ PHPSESSID); If ($ out = false | $ out = NULL) {return '';} return $ Out;} public static function write ($ PHPSESSID, $ data) {$ method = $ data? 'Set': 'replace '; return self: $ Handler-> $ method (SELF: session_key ($ PHPSESSID), $ data, memcache_compressed, self :: $ lifetime);} public static function destroy ($ PHPSESSID) {return self ::$ Handler-> Delete (SELF: session_key ($ PHPSESSID ));} public static function GC ($ lifetime) {return true;} Private Static function session_key ($ PHPSESSID) {$ session_key = self: ns. $ PHPSESSID; return $ session_key; }}$ memcac He = new memcache; $ memcache-> connect ("localhost", 11211) or die ("cocould not connect memcache"); memsession: Start ($ memcache ); /* foreach ($ options ['servers'] as $ server) {$ this-> Handler-> $ addserver ($ server ['IP'], $ server ['Port']); // echo $ server ['IP'];} */?>

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.