PHP代码
- <?php
- class session {
- public $lifeTime;
- private $db;
- private $table;
- function __construct($db, $table)
- {
- $this->db=$db;
- $this->table=$table;
- }
- function open($savePath, $sessName)
- {
- $this->lifeTime = get_cfg_var("session.gc_maxlifetime");
- if(!$dbHandle || !$dbSel)return false;
- return true;
- }
- function close()
- {
- $this->gc(ini_get(‘session.gc_maxlifetime’));
- return ture;
- }
- function read($sessID)
- {
- $res = $this->db->query("SELECT session_data AS d FROM $this->table
- WHERE session_id = ‘$sessID’
- AND session_expires > ".time());
- if($row = $res->fetch())return $row[‘d’];
- return "";
- }
- function write($sessID,$sessData)
- {
- $newExp = time() + $this->lifeTime;
- $res = $this->db->query("SELECT count(*) FROM $this->table WHERE session_id = ‘$sessID’");
- if($res->fetchColumn())
- {
- $aff = $this->db->exec("UPDATE $this->table
- SET session_expires = ‘$newExp’,
- session_data = ‘$sessData’
- WHERE session_id = ‘$sessID’");
- if($aff)return true;
- }
- else
- {
- $aff = $this->db->exec("INSERT INTO $this->table (
- session_id,
- session_expires,
- session_data)
- VALUES(
- ‘$sessID’,
- ‘$newExp’,
- ‘$sessData’)");
- if($aff)return true;
- }
- return false;
- }
- function destroy($sessID)
- {
- $aff = $this->db->exec("DELETE FROM $this->table WHERE session_id = ‘$sessID’");
- if($aff)return true;
- return false;
- }
- function gc($sessMaxLifeTime)
- {
- $aff = $this->db->exec("DELETE FROM $this->table WHERE session_expires < ".time(),$this->dbHandle);
- return $aff;
- }
- }
- $pdo_session = new session($db1, ‘user_sessions’);
- session_set_save_handler(array($pdo_session,"open"),
- array($pdo_session,"close"),
- array($pdo_session,"read"),
- array($pdo_session,"write"),
- array($pdo_session,"destroy"),
- array($pdo_session,"gc"));
- ?>