通過(guò)session_set_save_handler(string open,string close,string read,string write,string destory,string gc)鳍贾。
(1)open(save path ,session_name)----找到session存儲(chǔ)的地址,
(2)close()------關(guān)閉數(shù)據(jù)庫(kù)
(3)read(key)-------讀取session值蕉饼,key對(duì)應(yīng)session_id
(4)write(key,data)------data對(duì)應(yīng)設(shè)置的session變量
(5)destory(key)------注銷session對(duì)應(yīng)的
(6)gc(expriy_time)------清除過(guò)期的session
簡(jiǎn)單應(yīng)用
<?php
$con = mysql_connect("127.0.0.1", "user" , "pass");
mysql_select_db("session");
function open($save_path, $session_name) {
return(true);
}
function close() {
return(true);
}
function read($id) {
if ($result = mysql_query("select * from session where id='$id'")) {
if ($row = mysql_felth_row($result)) {
return $row["data"];
}
} else {
return "";
}
}
function write($id, $sess_data) {
if ($result = mysql_query("update session set data='$sess_data' where id='$id'")) {
return true;
} else {
return false;
}
}
function destroy($id) {
if ($result = mysql_query("delete * from session where id='$id'")) {
return true;
} else {
return false;
}
}
function gc($maxlifetime) {
return true;
}
session_set_save_handler("open", "close", "read", "write", "destroy", "gc");
session_start();
// proceed to use sessions normally
實(shí)際案例
<?php
/**
* SessionMysql 數(shù)據(jù)庫(kù)存儲(chǔ)類
*/
defined('IN_QIAN') or exit('Access Denied');
class SessionMysql {
public $lifetime = 1800; // 有效期拷泽,單位:秒(s)贺待,默認(rèn)30分鐘
public $db;
public $table;
/**
* 構(gòu)造函數(shù)
*/
public function __construct() {
$this->db = Base::loadModel('SessionModel');
$this->lifetime = Base::loadConfig('system', 'session_lifetime');
session_set_save_handler(
array(&$this, 'open'), // 在運(yùn)行session_start()時(shí)執(zhí)行
array(&$this, 'close'), // 在腳本執(zhí)行完成 或 調(diào)用session_write_close() 或 session_destroy()時(shí)被執(zhí)行,即在所有session操作完后被執(zhí)行
array(&$this, 'read'), // 在運(yùn)行session_start()時(shí)執(zhí)行,因?yàn)樵趕ession_start時(shí)除抛,會(huì)去read當(dāng)前session數(shù)據(jù)
array(&$this, 'write'), // 此方法在腳本結(jié)束和使用session_write_close()強(qiáng)制提交SESSION數(shù)據(jù)時(shí)執(zhí)行
array(&$this, 'destroy'), // 在運(yùn)行session_destroy()時(shí)執(zhí)行
array(&$this, 'gc') // 執(zhí)行概率由session.gc_probability 和 session.gc_divisor的值決定薯酝,時(shí)機(jī)是在open半沽,read之后,session_start會(huì)相繼執(zhí)行open吴菠,read和gc
);
session_start(); // 這也是必須的者填,打開session,必須在session_set_save_handler后面執(zhí)行
}
/**
* session_set_save_handler open方法
*
* @param $savePath
* @param $sessionName
* @return true
*/
public function open($savePath, $sessionName) {
return true;
}
/**
* session_set_save_handler close方法
*
* @return bool
*/
public function close() {
return $this->gc($this->lifetime);
}
/**
* 讀取session_id
*
* session_set_save_handler read方法
* @return string 讀取session_id
*/
public function read($sessionId) {
$condition = array(
'where' => array(
'session_id' => $sessionId
),
'fields' => 'data'
);
$row = $this->db->fetchFirst($condition);
return $row ? $row['data'] : '';
}
/**
* 寫入session_id 的值
*
* @param $sessionId 會(huì)話ID
* @param $data 值
* @return mixed query 執(zhí)行結(jié)果
*/
public function write($sessionId, $data) {
$userId = isset($_SESSION['userId']) ? $_SESSION['userId'] : 0;
$roleId = isset($_SESSION['roleId']) ? $_SESSION['roleId'] : 0;
$grouId = isset($_SESSION['grouId']) ? $_SESSION['grouId'] : 0;
$m = defined('ROUTE_M') ? ROUTE_M : '';
$c = defined('ROUTE_C') ? ROUTE_C : '';
$a = defined('ROUTE_A') ? ROUTE_A : '';
if (strlen($data) > 255) {
$data = '';
}
$ip = get_ip();
$sessionData = array(
'session_id' => $sessionId,
'user_id' => $userId,
'ip' => $ip,
'last_visit' => SYS_TIME,
'role_id' => $roleId,
'group_id' => $grouId,
'm' => $m,
'c' => $c,
'a' => $a,
'data' => $data,
);
return $this->db->insert($sessionData, 1, 1);
}
/**
* 刪除指定的session_id
*
* @param string $sessionId 會(huì)話ID
* @return bool
*/
public function destroy($sessionId) {
return $this->db->delete(array('session_id' => $sessionId));
}
/**
* 刪除過(guò)期的 session
*
* @param $lifetime session有效期(單位:秒)
* @return bool
*/
public function gc($lifetime) {
$expireTime = SYS_TIME - $lifetime;
return $this->db->delete("`last_visit`<$expireTime");
}
}