Session_set_save_handler text mode implementation_PHP tutorial

WBOY
Release: 2016-07-13 10:27:32
Original
1007 people have browsed it

1. Construct a session abstract class and implement the basic functions necessary for the Session_set_save_hanlder function

Session_set_save_handler text mode implementation_PHP tutorialphp /* Implement session storage operations using other forms such as files or databases * author:Libin * Data:2014-06-24 *Abstract top level */ abstract class session { /* open::Set storage path */ abstract public function open(); /* close::Close storage mode handle */ abstract public function close(); /* read:: Query the corresponding value according to the key, typical key-value pair */ abstract public function read($key); /* write::write session */ abstract public function write($key,$value); /* destory:: Destroy the session of a certain key */ abstract public function destroy($key); /* gc:: Recycling mechanism, clear session data that has expired for the specified time (minutes) */ abstract public function gc($min = 0); /* start::Enable session method rewriting */ abstract public function start(); } ?> View Code

2. Write a specific method implementation class for txt text mode

Session_set_save_handler text mode implementation_PHP tutorialphp /* Implement session storage operations using other forms such as files or databases * author:Libin * Data:2014-06-24 * Text (txt) implementation method */ class session_txt extends session { /* Variable storage */ private $save_path = '';//session save 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 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 recycling return true; } /* close::Close storage mode handle */ final public function close(){ return true; } /* read:: Query the corresponding value according to 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 public function write($key,$value){ $this->set(__function__,$key); return @fwrite($this->save_hand,$value); } /* destory:: Destroy the session of a certain key */ final public function destroy($key){ $this->set(__function__,$key); return @unlink($this->file_path); } /* gc:: Recycling mechanism, clear session data that has expired for the specified time (minutes) */ final public function gc($min = 10){ $hand = @opendir($this->save_path);//Open folder while($file = @readdir($hand)){//Traverse the file Clip if(is_file($this->save_path.'/'.$file)){//If it is a file, exclude.||.. $fileatime = fileatime($this->save_path.'/'.$file);//Get the last modification time of the file if(((time() - $fileatime) / 60) > $min){//If the time difference is greater than $min minutes unlink($this->save_path.'/'.$file);//Perform file cleaning function } }} return true; } ################################################################################# /* MAGIC __SET */ final public function __set($key,$value){ $this->$key = $value; return true; } /* 获取存储句柄 */ final private function set($mode = 'read',$key){ $this->file_path = $this->save_path.'/'.$this->save_prefix.$key;//设置文件路径 if($mode != 'read' && $mode != 'write'){return false;}; $mode = $mode == 'write' ? 'w' : 'r';//读或写 $this->save_hand = @fopen($this->file_path,$mode); } /* start::开启session方法重写 */ 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、引入session.php,实现工厂模式最后一步

<?<span>php
    </span><span>/*</span><span> 实现session利用文件形式或者数据库等其它形式进行存储操作
     * author:Libin
     * Data:2014-06-24
     * 工厂模式
    </span><span>*/</span>
    <span>class</span><span> _session_start {
        </span><span>/*</span><span> 构造函数 </span><span>*/</span>
        <span>static</span> <span>public</span> <span>function</span> __start(<span>$mode</span> = 'txt'<span>){
            </span><span>require_once</span>('session.class.php');<span>//</span><span>加载抽象方法</span>
            <span>if</span>(!<span>require_once</span>('function/'.<span>$mode</span>.'.php'<span>)){
                </span><span>return</span> <span>false</span><span>;
            }</span><span>else</span><span>{
                </span><span>$session_handle</span>     = 'session_'.<span>$mode</span><span>;
                </span><span>$handler</span>             = <span>new</span> <span>$session_handle</span><span>();
                </span><span>$handler</span>-><span>start();
            }
        }
    }
    _session_start</span>::__start('txt');<span>//</span><span>重写session函数开始</span>
?>
Copy after login

PS::明儿个编写mysql实现此功能,今天就到此为止啦啦啦!

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/817467.htmlTechArticle1、构建session抽象类,实现Session_set_save_hanlder函数必须的基础函数 ? php /* 实现session利用文件形式或者数据库等其它形式进行存储操作 * au...
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!