我在设置 session_set_save_handler 时遇到问题。我将 php.ini 配置为 session.handler = user
这个简单的测试失败了:
//Define custom session handler
if(session_set_save_handler("sess_open", "sess_close", "sess_read", "sess_write", "sess_destroy", "sess_gc")){
die('set fine');
}else{
die('Couldn\'t set session handler');
这是我的会话课。
//Constructor
function __construct(){
//Define custom session handler
if(session_set_save_handler("sess_open", "sess_close", "sess_read", "sess_write", "sess_destroy", "sess_gc")){
die('set fine');
}else{
die('Couldn\'t set session handler');
}
//Start session
session_start();
}
//Custom session functions
function sess_open($sess_path, $sess_name) {
return true;
}
function sess_close() {
return true;
}
function sess_read($sess_id) {
//Query for session record in
$results = $db->QuerySingleRow("SELECT data FROM sessions WHERE session_id = '$sess_id'");
//Check that record is returned
if ($results != false)
{
//Session found, pull out data field value
$sess_data = $results->data;
//Grab current time
$CurrentTime = time();
//Update session record with current timestamp
$db->Query("UPDATE sessions SET last_updated = $CurrentTime WHERE session_id = '$sess_id'");
//Return
return $sess_data;
}
else
{
//No session found
//Grab current timestamp
$CurrentTime = time();
//Insert new session to DB
$db->Query("INSERT INTO sessions (session_id, last_updated) VALUES ('$sess_id', $CurrentTime)");
//Return blank per nature of session_set_save_handler read()
return '';
}
}
function sess_write($sess_id, $data) {
//Grab current timestamp
$CurrentTime = time();
//Update session record to hold new data and update last_updated field
$db->Query("UPDATE sessions SET data = '$data', last_updated = $CurrentTime WHERE session_id = '$sess_id'");
return true;
}
function sess_destroy($sess_id) {
//Delete session from DB
$db->Query("DELETE FROM sessions WHERE session_id = '$sess_id'");
return true;
}
function sess_gc($sess_maxlifetime) {
//Get current timestamp
$CurrentTime = time();
//Delete from session based on garbage collection
$db->Query("DELETE FROM sessions WHERE last_updated < $CurrentTime");
return true;
}
}
我唯一能想到的是 $db 是我的 MySQL DB 类的一个对象,但我不能包含该类然后创建它的实例。
我不想在 DB 类上重新发明轮子,所以我从 Jeff Williams 那里得到了它:http ://www.phpclasses.org/package/3698-PHP-MySQL-database-access-wrapper.html
我试图将它包含在类主体之外然后页面不呈现,只是一个空白的白色页面没有错误。:
<?php
include 'mysql.class.php';
$db = new MySQL(true);
class session
{
//Constructor
function __construct(){
....