????session??дmysql
????????? session_start();???????????????????new SessionDB();
session_set_save_handler(
array($this?? 'userSessionBegin')??
array($this?? 'userSessionEnd')??
array($this?? 'userSessionRead')??
array($this?? 'userSessionWrite')??
array($this?? 'userSessionDelete')??
array($this?? 'userSessionGC')
);??????????????д????????????????????????????????????д
session_set_save_handler(
'userSessionBegin'??
'userSessionEnd'??
'userSessionRead'??
'userSessionWrite'??
'userSessionDelete'??
'userSessionGC')
<?php
/**
* session???????
*/
class SessionDB {
private $_dao;
public function __construct() {
//????session??????
ini_set('session.save_handler'?? 'user');
session_set_save_handler(
array($this?? 'userSessionBegin')??
array($this?? 'userSessionEnd')??
array($this?? 'userSessionRead')??
array($this?? 'userSessionWrite')??
array($this?? 'userSessionDelete')??
array($this?? 'userSessionGC')
);
//????
session_start();
}
function userSessionBegin() {
//?????DAO
$config = array('host' => '127.0.0.1'??    'port' => '3306'?? 'username'=>'shop34'?? 'password' => '1234abcd'?? 'charset'=>'utf8'?? 'dbname'=>'shop34');
$this->_dao = MySQLDB::getInstance($config);
}
function userSessionEnd() {
return true;
}
/**
* ??????
* ????????    session?????????????
* ??????        ????session?????????????
* @param $sess_id string
* @return string
*/
function userSessionRead($sess_id) {
//???
$sql = "SELECT session_content FROM `p34_session` WHERE session_id='$sess_id'";
return (string) $this->_dao->getOne($sql);
}
/**
* д????
* ????????    ?????????????PHP????????β?
* ??????        ??????????????session??????????洢????????У?
* @param $sess_id string
* @param $sess_content string ???л????session?????????
* @return bool
*/
function userSessionWrite($sess_id?? $sess_content) {
// ???д
$sql = "REPLACE INTO `p34_session` VALUES ('$sess_id'?? '$sess_content'?? unix_timestamp())";
return $this->_dao->query($sql);
}
/**
* ???????
* ????????    ??????session_destroy()????session?????б?????
* ??????        ??????session???????????????
* @param $sess_id string
* @return bool
*/
function userSessionDelete($sess_id) {
//???
$sql = "DELETE FROM `p34_session` WHERE session_id='$sess_id'";
return $this->_dao->query($sql);
}
/**
* ???????????
* ????????    ????session????????и???????
* ??????        ?????Щ?????session??????
* @param $max_lifetime
* @return bool
*/
function userSessionGC($max_lifetime) {
//???
$sql = "DELETE FROM `p34_session` WHERE last_time<unix_timestamp()-$max_lifetime";
return $this->_dao->query($sql);
}
}
????gc???????????????д
????ini_set('session.gc_probability'?? '1');
????ini_set('session.gc_divisor'?? '3');
????//session.gc_divisor ?? session.gc_probability ???????????????????????????? gc??garbage collection ?????????????????????????? gc_probability/gc_divisor ????????????? 1/100 ??ζ??????????????? 1% ???????? gc ?????session.gc_divisor ???? 100??