本文介绍了Session_set_save_handler未设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在设置session_set_save_handler时遇到问题.我将php.ini配置为session.handler = user

I'm having issues with setting session_set_save_handler. I configured my php.ini to 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类的对象,但是我不能包含该类,然后创建它的实例.

The only thing I can think of is $db is an suppose to be an object of my MySQL DB class but I can't include the class and then create an instance of it.

我不想重新发明DB类,所以我在这里从Jeff Williams手中抢了下来: http://www.phpclasses.org/package/3698-PHP-MySQL-database-access-wrapper.html

I didn't want to reinvent the wheel on the DB class so I grabbed it from Jeff Williams here: http://www.phpclasses.org/package/3698-PHP-MySQL-database-access-wrapper.html

我尝试将其包含在类正文之外,然后页面无法呈现,只是空白的白色页面,没有错误.

I've tried to include it out side the class body put then the page doesn't render, just a blank white page no errors.:

<?php
include 'mysql.class.php';
$db = new MySQL(true);

class session
{

//Constructor
function __construct(){
....

推荐答案

设置会话保存处理程序失败:

Setting the session save handler fails:

session_set_save_handler("sess_open", "sess_close", "sess_read", "sess_write", "sess_destroy", "sess_gc")

由于您要注册的这些回调不存在:

Because these callbacks you want to register to do not exists:

var_dump(is_callable("sess_open")); # FALSE

这是因为您的对象方法需要正确地注册为回调.对象方法回调以具有两个元素的数组的形式编写,第一个是对象,第二个是方法名的字符串.来自PHP网络的示例与您的示例类似:

That is because your object methods needs to be properly registered as callbacks. An object method callback is written in form of an array with two elements, the first one is the object, the second one a string of the methodname. Example from PHP net that is similar to yours:

$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.

As you can see, each method is written as a single array with the first element $handler always.

在类中,您可以使用$this引用同一对象.但是,在完全编写自己的内容之前,请检查 session_set_save_handler() PHP手册页信息,示例和用户贡献的注释.您可以采用不同的方式来组织案件.

From within the class you can use $this to refer to the same object. But before your fully write your own, check the session_set_save_handler() PHP manual page for infos, examples and user contributed notes. There are different ways how you can organize that for your case.

这篇关于Session_set_save_handler未设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 00:05