1. Construct a session abstract class to implement the basic functions required by the Session_set_save_hanlder function.
<? Php/* implements session storage operations using file or database or other forms * author: Libin * Data: * abstract top layer */abstract class session {/* open :: set the storage path */abstract public function open ();/* close: close the storage mode handle */abstract public function close ();/* read :: query the corresponding value based on the key. A typical key-value Pair */abstract public function read ($ key);/* write: write session */abstract public function write ($ key, $ value);/* destory: destroy the session of a key */abstract public f Unction destory ($ key);/* gc: indicates the recycle mechanism to clear session data that expires at a specified time (in minutes) */abstract public function gc ($ min = 0 ); /* start: Enable the session method to override */abstract public function start () ;}?>View Code
2. Compile the txt text mode method implementation class
<? Php/* implements session storage operations using the file format or database and other forms * author: Libin * Data: * text (txt) implementation Method */class session_txt extends session {/* variable storage */private $ save_path = ''; // session storage path private $ file_path = ''; // session file path private $ save_name = ''; // session name private $ save_hand = null; // resource handle private $ save_prefix = 'mysession _'; // session prefix/* open: Set the storage path */final public function open () {$ this-> save_path = 'd :\ Session '; if (! Is_dir ($ this-> save_path) {if (! Mkdir ($ this-> save_path) {return false ;}}$ this-> gc (); // enable the recycle mechanism return true;}/* close :: close storage mode handle */final public function close () {return true;}/* read: query the corresponding value based on the key, typical key-Value Pair */final public function read ($ key) {$ this-> set (_ function __, $ key ); $ data = @ fread ($ this-> save_hand, filesize ($ this-> file_path); if (empty ($ data) | $ data = '') {return '';} return $ data;}/* write: write session */final pu Blic function write ($ key, $ value) {$ this-> set (_ function __, $ key); return @ fwrite ($ this-> save_hand, $ value);}/* destory: destroy the session of a key */final public function destory ($ key) {$ this-> set (_ function __, $ key); return @ unlink ($ this-> file_path);}/* gc: reclaim mechanism, clear expiration time (minutes) session data */final public function gc ($ min = 10) {$ hand = @ opendir ($ this-> save_path ); // open the folder while ($ file = @ readdir ($ hand) {// traverse the folder if (Is_file ($ this-> save_path. '/'. $ file) {// exclude files. | .. $ fileatime = fileatime ($ this-> save_path. '/'. $ file); // obtain the last modification time of a file. if (time ()-$ fileatime)/60)> $ min) {// if the time difference is greater than $ min minutes unlink ($ this-> save_path. '/'. $ file); // execute the file cleanup function }}return true ;} ######################################## ######################################## #/* MAGIC _ SET */final public function _ set ($ key, $ value) {$ this-> $ key = $ v Alue; return true;}/* Get the storage handle */final private function set ($ mode = 'read', $ key) {$ this-> file_path = $ this-> save_path. '/'. $ this-> save_prefix. $ key; // set the file path if ($ mode! = 'Read' & $ mode! = 'Write') {return false ;}; $ mode = 'write '? 'W': 'R'; // read or write $ this-> save_hand = @ fopen ($ this-> file_path, $ mode);}/* start :: enable the session method to override */final public function start () {@ session_set_save_handler (array ($ this, 'open'), array ($ this, 'close '), array ($ this, 'read'), array ($ this, 'write'), array ($ this, 'destory '), array ($ this, 'gc '); ob_start (); session_start (); ob_end_flush () ;}}?>View Code
3. Introduce session. php to implement the factory mode.
<? Php/* implements session storage operations using the file format, database, and other Forms * author: Libin * Data: 2014-06-24 * factory mode */class _ session_start {/* constructor */static public function _ start ($ mode = 'txt ') {require_once ('session. class. php '); // load the abstract method if (! Require_once ('function /'. $ mode. '. php ') {return false;} else {$ session_handle = 'session _'. $ mode; $ handler = new $ session_handle (); $ handler-> start () ;}}_ session_start ::__ start ('txt '); // start the rewrite session function?>
PS: minger writes mysql to implement this function. So far today!