1、構建session抽象類別,實現Session_set_save_hanlder函數必須的基礎函數
php
/* 實現session利用檔案形式或者資料庫等其它形式進行儲存操作 * author:Libin * Data:2014-06-24 * 抽象頂層 */ abstract class session { /* open::設定儲存路徑 */ abstract public function open(); /* close::關閉儲存模式控制代碼 */ abstract public function close(); /* read::根據key查詢對應的value,典型的索引值對 */ abstract public function read(
$key); /* write::寫session */ abstract public function write(
$key,
$value); /* destory::銷毀某key的session */ abstract public function destory(
$key); /* gc::回收機制,清除到期指定時間(分鐘)session資料 */ abstract public function gc(
$min = 0
); /* start::開啟session方法重寫 */ abstract public function start(); }?>
View Code
2、編寫txt文字模式具體方法實作類別
php
/* 實現session利用檔案形式或者資料庫等其它形式進行儲存操作 * author:Libin * Data:2014-06-24 * 文本(txt)實現方法 */ class session_txt
extends session { /* 變數儲存 */ private $save_path = '';
//session儲存路徑 private $file_path = '';
//session檔案路徑 private $save_name = '';
//session命名 private $save_hand =
null;
//資源控制代碼 private $save_prefix = 'mysession_';
//session首碼 /* open::設定儲存路徑 */ final public function open(){ $this->save_path = 'D:\session'
; if(!
is_dir(
$this->
save_path)){ if(!
mkdir(
$this->
save_path)){ return false; }; } $this->gc();
//啟用回收機制 return true; } /* close::關閉儲存模式控制代碼 */ final public function close(){ return true; } /* read::根據key查詢對應的value,典型的索引值對 */ 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::寫session */ final public function write(
$key,
$value){ $this->set(
__function__,
$key); return @
fwrite(
$this->save_hand,
$value); } /* destory::銷毀某key的session */ final public function destory(
$key){ $this->set(
__function__,
$key); return @
unlink(
$this->
file_path); } /* gc::回收機制,清除到期指定時間(分鐘)session資料 */ final public function gc(
$min = 10
){ $hand = @
opendir(
$this->save_path);
//開啟檔案夾 while(
$file = @
readdir(
$hand)){
//遍曆檔案夾 if(
is_file(
$this->save_path.'/'.
$file)){
//如果為檔案,排除.||.. $fileatime =
fileatime(
$this->save_path.'/'.
$file);
//擷取檔案最後修改時間 if(((
time() -
$fileatime) / 60) >
$min){
//如果時間相差大於$min分鐘 unlink(
$this->save_path.'/'.
$file);
//執行檔案清理功能 } } } 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,實現原廠模式最後一步
php /* 實現session利用檔案形式或者資料庫等其它形式進行儲存操作 * author:Libin * Data:2014-06-24 * 原廠模式 */ class _session_start { /* 建構函式 */ static public function __start($mode = 'txt'){ require_once('session.class.php');//載入抽象方法 if(!require_once('function/'.$mode.'.php')){ return false; }else{ $session_handle = 'session_'.$mode; $handler = new $session_handle(); $handler->start(); } } } _session_start::__start('txt');//重寫session函數開始?>
PS::明兒個編寫mysql實現此功能,今天就到此為止啦啦啦!
http://www.bkjia.com/PHPjc/817467.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/817467.htmlTechArticle1、構建session抽象類別,實現Session_set_save_hanlder函數必須的基礎函數 ? php /* 實現session利用檔案形式或者資料庫等其它形式進行儲存操作 * au...