我遇到设置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类的一个对象,但我不能包含该类,然后创建它的实例。
我不想重新发明数据库类,所以我从杰夫威廉姆斯那里抓住它: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(){
....
答案 0 :(得分:2)
设置会话保存处理程序失败:
session_set_save_handler("sess_open", "sess_close", "sess_read", "sess_write", "sess_destroy", "sess_gc")
因为您要注册的这些回调不存在:
var_dump(is_callable("sess_open")); # FALSE
这是因为您的对象方法需要正确注册为回调。对象方法回调以具有两个元素的数组的形式写入,第一个是对象,第二个是方法名的字符串。来自PHP net的示例与您的类似:
$handler = new FileSessionHandler();
session_set_save_handler(
array($handler, 'open'),
array($handler, 'close'),
array($handler, 'read'),
array($handler, 'write'),
array($handler, 'destroy'),
array($handler, 'gc')
);
如您所见,每个方法都被编写为单个数组,第一个元素始终为$handler
。
在课程中,您可以使用$this
来引用同一个对象。但在您完全自己编写之前,请查看session_set_save_handler()
PHP manual page的信息,示例和用户提供的注释。您可以通过不同的方式为您的案例进行组织。
答案 1 :(得分:1)
如果您在构造函数中使用该函数,那么您需要传递$this
,如下所示:
session_set_save_handler(
array($this, 'sess_open'),
array($this, 'sess_close'),
array($this, 'sess_read'),
array($this, 'sess_write'),
array($this, 'sess_destroy'),
array($this, 'sess_gc')
);
然后实例化类
new SessionClass;
如果您有疑问,可以随时查看the documentation。一定要阅读评论;他们通常非常有帮助。