我有以下代码,基本上来自http://www.spiration.co.uk/post/1333/PHP-5-sessions-in-mysql-database-with-PDO-db-objects
让我感到有点奇怪的是,那里没有任何东西可以关闭数据库连接(即设置 $this->db = null)。我应该担心这个(或这里的其他任何事情)吗?具体来说,我应该把 $this->db = null 放在 close() 函数中还是 close() 这里有别的意思?:)
public $db;
public $maxlifetime = 1800; /* 30 mins */
public $expiry;
public function __destruct(){
session_write_close();
}
public function open( $path, $name ) {
$this->db = new PDO('mysql:host=' . MySQLConfigClass::MySql_SERVERNAME . ';dbname=' . MySQLConfigClass::MySql_DBNAME, MySQLConfigClass::MySql_LOGINNAME, MySQLConfigClass::MySql_PASS);
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return true;
}
public function close() {
return true;
}
public function read($se_id){
$qry = "select se_value from sessions where se_id = '$se_id' and se_expires > " . time();
$sth = $this->db->prepare($qry);
$sth->execute();
$result = $sth->fetch(PDO::FETCH_ASSOC);
return $result['se_value'];
}
public function write($se_id, $se_val){
$this->expiry = time() + $this->maxlifetime;
try {
$qry= "insert into sessions (se_id, se_value, se_expires) values('$se_id', '$se_val', $this->expiry)";
$sth = $this->db->prepare($qry);
$sth->execute();
} catch (PDOException $e) {
$qry= "update sessions set se_value='$se_val', se_expires=$this->expiry where se_id='$se_id'";
$sth = $this->db->prepare($qry);
$sth->execute();
}
}
public function destroy($se_id){
$qry = "delete from sessions where se_id ='$se_id'";
$sth = $this->db->prepare($qry);
$tot= $sth->execute();
return ($tot);
}
public function gc($maxlifetime){
$qry = "delete from sessions where se_expires < ".time();
$sth = $this->db->prepare($qry);
$tot= $sth->execute();
return ($tot);
}
}
$session = new Session;
session_set_save_handler(
array(&$session, "open"),
array(&$session, "close"),
array(&$session, "read"),
array(&$session, "write"),
array(&$session, "destroy"),
array(&$session, "gc")
);
session_start();