自定義session儲存 FILE 方式類
在php.ini配置文件中更改設置 (Registered_save_handlers 有三種方式 files user memcache)
session.save_handler = user 表示用戶自定義session類
<?php /* * 自定義session儲存File方式的類 * 打開 * * 關閉 * * 讀取 * * 寫入 * * 銷燬 * * 回收 * */ class FileSession{ private static $path = "d:/wamp/sfile/"; public static function start($path= "d:/wamp/sfile/"){ self::$path = $path; //註冊自定義函數 session_set_save_handler( array(__CLASS__,'open'), array(__CLASS__,'close'), array(__CLASS__,'read'), array(__CLASS__,'write'), array(__CLASS__,'destroy'), array(__CLASS__, 'gc')); //開啓會話 session_start(); } //開始 sesssion_start() public static function open($path,$name){ echo 'open<br/>'; return true; } //關閉 public static function close(){ echo 'close<br/>'; return true; } //讀取 echo public static function read($sid){ echo 'read<br/>'; $filename = self::$path.'yang_'.$sid; // return 'username|s:12:"津沙港灣";age|i:33;sex|s:4:"male";'; return @file_get_contents($filename); } //寫入 $_SESSION['username']='yang' public static function write($sid,$data){ echo 'write<br/>'; $filename = self::$path.'yang_'.$sid; echo $filename.'<br/>'; echo $data.'<br/>'; return file_put_contents($filename, $data); } //銷燬 session_destroy public static function destroy($sid){ echo 'destroy<br/>'; $filename = self::$path.'yang_'.$sid; echo $filename.'<br/>'; return @unlink($filename); } //回收垃圾 public static function gc($maxlifetime){ echo 'gc<br/>'; foreach(glob(self::$path.'yang_*') as $file){ echo $file.'<br/>'; //只刪除過期 if((filemtime($file)+$maxlifetime) < time()){ unlink($file); } } } } FileSession::start();