Session_set_save_handler: text mode implementation _ PHP Tutorial

Source: Internet
Author: User
Session_set_save_handler: text mode implementation. 1. create a session abstract class to implement the basic functions required by the Session_set_save_hanlder function? Php * implements session storage operations in the form of files or databases * au 1. constructs session Abstract classes to implement the basic functions required by the Session_set_save_hanlder function.

View Code

2. Compile the txt text mode method implementation class

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.

 Start () ;}}_ session_start ::__ start ('txt '); // rewrite the start of the session function?>

PS: Minger writes mysql to implement this function. so far today!

Why? Php/* implements session storage operations using the file format or database and other forms * au...

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.