Session_set_save_handler text Pattern Implementation _php Tutorial

Source: Internet
Author: User
1, constructs the session abstract class, realizes the Session_set_save_hanlder function must the basic function

Php /* implement session storage operations using file form or database and other forms * Author:libin * data:2014-06-24 * abstract Top level */ Abstract class Session { /* Open:: Set storage Path */ Abstract Public function open (); /* Close :: Closing the storage mode handle */ Abstract Public function close (); /* read:: According to the key query corresponding value, a typical key value pair */ Abstract Public functionRead $key ); /* write:: Writing session */ Abstract Public functionWrite $key, $value ); /* destory:: Destroying the session of a key */ Abstract Public functionDestory ( $key ); /* GC:: Recycle mechanism to clear expired time (minutes) session data */ Abstract Public functiongc $min= 0 ); /* Start:: Open Session method Override */ Abstract Public function start ();}?> View Code

2, write txt text mode concrete method implementation class

Php /* implement the session using file form or other forms of database, such as storage operations * Author:libin * data:2014-06-24 * TEXT (TXT) Implementation method */ classSession_txt extends Session { /* Variable Storage */ Private $save _path= ''; // Session Save Path Private $file _path= ''; // Session file Path Private $save _name= ''; // Session naming Private $save _hand= NULL; // Resource Handle Private $save _prefix= ' Mysession_ '; // Session prefix /* Open:: Set 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 recycle mechanism return true ; } /* Close :: Closing the storage mode handle */ Final Public function Close () { return true ; } /* read:: According to the key query corresponding value, a typical key value pair */ Final Public functionRead $key ){ $this->set ( __function__, $key ); $data= @ fread( $this->save_hand, filesize( $this- file_path)); if( Empty( $data) || $data== '' ){ return'' ; } return $data ; } /* write:: Writing session */ Final Public functionWrite $key, $value ){ $this->set ( __function__, $key ); return@ fwrite( $this->save_hand, $value ); } /* destory:: Destroying the session of a key */ Final Public functionDestory ( $key ){ $this->set ( __function__, $key ); return@ unlink( $this- File_path); } /* GC:: Recycle mechanism to clear expired time (minutes) session data */ Final Public functiongc $min= 10 ){ $hand= @ Opendir( $this->save_path); // Open Folder while( $file= @ Readdir( $hand)){ // Traverse Folder if( Is_file( $this->save_path. ' /'. $file)){ // If for file, exclude. | | $fileatime= Fileatime( $this->save_path. ' /'. $file); // get file Last modified time if((( Time() - $fileatime)/> $min){ // If the time difference is greater than $min minutes unlink( $this->save_path. ' /'. $file); // perform file cleanup function } } } return true ; } # ################################################################################ /* MAGIC __set */ Final Public function__set ( $key, $value ){ $this- $key= $value ; return true ; } /* Get storage handle */ Final Private functionSet $mode= ' Read ', $key ){ $this->file_path = $this->save_path. ' /'. $this->save_prefix. $key; // Set File path if( $mode! = ' read ' && $mode! = ' Write ') { return false ;}; $mode= $mode= = ' Write '? ' W ': ' R '; // Read or write $this->save_hand = @ fopen( $this->file_path, $mode ); } /* Start:: Open Session method 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, realize the last step of Factory mode

 Php/*implement the session using file form or other forms of database, such as storage operations * Author:libin * DATA:2014-06-24 * Factory mode*/    class_session_start {/*constructor Function*/        Static  Public function__start ($mode= ' txt '){            require_once(' session.class.php ');//Loading abstract methods            if(!require_once(' function/'.$mode.'. Php)){                return false; }Else{                $session _handle= ' Session_ '.$mode; $handler=New $session _handle(); $handler-start (); } }} _session_start:: __start (' txt ');//rewrite session function start?>

PS:: Tomorrow write MySQL to achieve this function, today is the end of the day!

http://www.bkjia.com/PHPjc/817467.html www.bkjia.com true http://www.bkjia.com/PHPjc/817467.html techarticle 1, constructs the session abstract class, realizes the Session_set_save_hanlder function must the basic function? PHP/* Implements the session using the file form or database and other forms of storage operations * AU ...

  • Related Article

    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.