有人可以帮我让自定义会话处理程序工作吗?
这似乎成功了一半,在某些页面上它将会话写入数据库,但在其他页面上写入没有数据的会话。此外 print_r($_SESSION) 不再显示任何会话变量。
数据库表:
id - varchar(32 - NULL No - Default None - Primary Key
access - int(10) - unsigned - NULL Yes - Default NULL
data - text - NULL Yes - Default NULL
前端控制器(定制框架)
require_once('../application/models/sessionfn.php');
session_set_save_handler('_open','_close','_read','_write','_destroy','_clean');
session_start();
在 sessionfn.php
function _open()
{
$config = Configuration::getInstance();
$host=$config->get("dbhost");
$dbname=$config->get("db");
$user=$config->get("dbuser");
$pass=$config->get("dbpass");
$dbh = new PDO("mysql:host=$host;dbname=$dbname",$user,$pass);
}
function _close()
{
$dbh = null;
}
function _read($id)
{
$config = Configuration::getInstance();
$host=$config->get("dbhost");
$dbname=$config->get("db");
$user=$config->get("dbuser");
$pass=$config->get("dbpass");
$dbh = new PDO("mysql:host=$host;dbname=$dbname",$user,$pass);
$sth = $dbh->prepare("SELECT data FROM sessions WHERE id = '$id'");
$sth->execute();
}
function _write($id, $data)
{
$config = Configuration::getInstance();
$host=$config->get("dbhost");
$dbname=$config->get("db");
$user=$config->get("dbuser");
$pass=$config->get("dbpass");
$dbh = new PDO("mysql:host=$host;dbname=$dbname",$user,$pass);
$access = time();
$sth = $dbh->prepare("REPLACE INTO sessions VALUES ('$id', '$access', '$data')");
$sth->execute();
}
function _destroy($id)
{
$config = Configuration::getInstance();
$host=$config->get("dbhost");
$dbname=$config->get("db");
$user=$config->get("dbuser");
$pass=$config->get("dbpass");
$dbh = new PDO("mysql:host=$host;dbname=$dbname",$user,$pass);
$sth = $dbh->prepare("DELETE FROM sessions WHERE id = '$id'");
$sth->execute();
}
function _clean($max)
{
$config = Configuration::getInstance();
$host=$config->get("dbhost");
$dbname=$config->get("db");
$user=$config->get("dbuser");
$pass=$config->get("dbpass");
$dbh = new PDO("mysql:host=$host;dbname=$dbname",$user,$pass);
$old = time() - $max;
$sth = $dbh->prepare("DELETE FROM sessions WHERE access < '$old'");
$sth->execute();
}