1. セッション抽象クラスを構築し、Session_set_save_hanlder 関数に必要な基本関数を実装します

php
/* ファイルやデータベースなどの他の形式を使用してセッション ストレージ操作を実装する
* 著者:リビン
* データ:2014-06-24
*抽象的なトップレベル
*/
要約 クラスセッション {
/* open::ストレージパスを設定 */
抽象 パブリック 関数 open();
/* close::ストレージモードハンドルを閉じる */
抽象 パブリック 関数 close();
/* read:: キー、典型的なキーと値のペアに従って、対応する値をクエリします */
abstract public function read(
$key);
/* write::write セッション */
abstract public function write(
$key,
$value);
/* destroy:: 特定のキーのセッションを破棄します */
抽象 パブリック 関数 destroy(
$key);
/* gc:: リサイクルメカニズム、指定された時間 (分) で期限切れになったセッションデータをクリアします */
抽象 パブリック 関数 gc(
$min = 0
);
/* start::セッションメソッドの書き換えを有効にする */
abstract public function start();
}
?>
コードを表示
2. txtテキストモード用の特定のメソッド実装クラスを作成します

php
/* ファイルやデータベースなどの他の形式を使用してセッション ストレージ操作を実装する
* 著者:リビン
* データ:2014-06-24
※テキスト(txt)実装方法
*/
クラス session_txt
延長セッション {
/* 可変ストレージ */
private $save_path = '';
//セッション保存パス
private $file_path = '';
//セッションファイルパス
プライベート $save_name = '';
//セッション名
private $save_hand =
null;
//リソースハンドル
private $save_prefix = 'mysession_';
//セッションプレフィックス
/* open::ストレージパスを設定 */
final public function open(){
$this->save_path = 'D:セッション'
;
if(!
is_dir(
$this->
save_path)){
if(!
mkdir(
$this->
save_path)){
戻る false;
};
}
$this->gc();
//リサイクルメカニズムを有効にする
戻る 本当;
}
/* close::ストレージモードハンドルを閉じる */
final public function close(){
戻る 本当;
}
/* read:: キー、典型的なキーと値のペアに従って、対応する値をクエリします */
final public function read(
$key){
$this->set(
__function__,
$key);
$data = @
fread(
$this->save_hand,
filesize(
$this->
file_path));
if(
空(
$data) ||
$data == ''
){
戻る ''
;
}return $data;
}
/* write::write セッション */
final public function write(
$key,
$value){
$this->set(
__function__,
$key);
return @
fwrite(
$this->save_hand,
$value);
}
/* destroy:: 特定のキーのセッションを破棄します */
final public function destroy(
$key){
$this->set(
__function__,
$key);
return @
unlink(
$this->
file_path);
}
/* gc:: リサイクルメカニズム、指定された時間 (分) で期限切れになったセッションデータをクリアします */
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);
//ファイルクリーニング関数を実行
}
}}
戻る 本当;
}
########################################### # ##############################
/* マジック __セット */
final public function __set(
$key,
$value){
$this->
$key =
$value;
戻る 本当;
}
/* ストレージハンドルを取得します */
final private function set(
$mode = 'read',
$key){
$this->file_path =
$this->save_path.'/'.
$this->save_prefix.
$key;
//ファイルパスを設定
if(
$mode != '読み取り' &&
$mode != '書き込み'){
return false;};
$mode =
$mode == 'write' ? 'w' : 'r';
//読み取りまたは書き込み
$this->save_hand = @
fopen(
$this->file_path,
$mode);
}
/* start::セッションメソッドの書き換えを有効にする */
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();
}
}
?>
コードを表示
3. ファクトリーモードの最後のステップを実現するために session.php を導入します
リーリー
追記: 明日は mysql を書いてこの関数を実装します。今日はここまでです!
http://www.bkjia.com/PHPjc/817467.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/817467.html技術記事 1. セッション抽象クラスを構築し、Session_set_save_hanlder 関数に必要な基本関数を実装しますか? php /* ファイルやデータベースなどの他の形式を使用してセッション ストレージ操作を実装します* au...