session_set_save_handler无效

时间:2013-02-22 17:55:27

标签: php wordpress session-set-save-handler

我一直在尝试使用session_set_save_handler()设置保存会话,我不知道是什么问题,我有所有可调用的函数,但是出错了,这里我发布了我的代码。顺便说一句,我在WP工作。

    <?php 
$root = dirname(dirname(dirname(dirname(__FILE__))));
if (file_exists($root.'/wp-load.php')) {
    require_once($root.'/wp-load.php');
}
if (file_exists($root.'/wp-config.php')) {
    require_once($root.'/wp-config.php');
}

global $wpdb;

//function called when session_start();
function _open() {
    print "Session opened.\n";
    print "</br>";
    return true;
}

//called at the end of the page
function _close() {
    print "</br>";
    print "Session closed.\n";
    print "</br>";
    return true;
}

//function called after session_start();
function _read($session_id) {
    global $wpdb;

    $session_id = mysql_real_escape_string($session_id);
    $record = $wpdb->get_var( "SELECT data FROM sessions_gm WHERE id='$session_id'");
    print "SELECT data FROM sessions_gm WHERE id='$session_id'";

    print "Session READ.\n";
    print "</br>";
    print "El record:".$record;

     if($record)
    return $record;

    return '';
}

function _write($session_id, $data) {
    global $wpdb;
    $access = time();
    $session_id = mysql_real_escape_string($session_id);
    $access = mysql_real_escape_string($access);
    $data = mysql_real_escape_string($data);

    $session = $wpdb->get_row("SELECT * FROM sessions_gm WHERE id = '$session_id'");

    print "SELECT * FROM sessions_gm WHERE id = '$session_id'";
    print "La session:";
    print_r($session);

    print "</br>";
    print "Session value written.\n";
    print "</br>";
    print "Sess_ID: $session_id\n";
    print "</br>";
    print "Data: $data\n\n";
    print "</br>";

    if($session == null)
    {
        $insert = $wpdb->insert( 
        'sessions_gm', 
        array( 
        'id' => $session_id, 
        'access' => $access,
        'data' => $data
        ), 
        array( 
        '%s', 
        '%d', 
        '%s'
        ) 
        );

        if($insert!=false)
        {
            print "Se inserto.";
            return true;
        }
    }
    else
    {
        $update = $wpdb->update( 
            'sessions_gm', 
            array( 
            'access' => $access,    // string
            'data' => $data // integer (number) 
            ), 
            array( 'id' => $session_id ), 
            array( 
            '%d',   // value1
            '%s'    // value2
            ), 
            array( '%s' ) 
        );

        if($update!=false)
        {
            print "Se updateo.";
            return true;
        }
    }

    return false;
}

// called when session_destroy();
function _destroy($session_id) {
    global $wpdb;
    $session_id = mysql_real_escape_string($session_id);
    print "</br>";
    print "Session destroy called.\n";
    print "</br>";
    $delete=$wpdb->query( "DELETE FROM sessions_gm WHERE id = '$session_id'");
    if($delete!=false)
    {
        print "Se elimino";
        return true;
    }
    return false;
}

function _gc($maxlifetime) {
    print "</br>";
    print "Session garbage collection called.\n";
    print "</br>";
    print "Sess_maxlifetime: $maxlifetime\n";
    print "</br>";
   $old = time() - $max;
   $old = mysql_real_escape_string($old);
   $delete=$wpdb->query( "DELETE FROM sessions_gm WHERE access < '$old'");

     if($delete!=false)
    {
        print "Se elimino las pasadas.";
        return true;
    }
    return false;
}

var_dump(is_callable("_open"));
var_dump(is_callable("_close"));
var_dump(is_callable("_read"));
var_dump(is_callable("_write"));
var_dump(is_callable("_destroy"));
var_dump(is_callable("_gc"));


if (session_set_save_handler("_open", "_close", "_read", "_write", "_destroy", "_gc")) {
    die('Works fine');
}
else {
    die('Couldn\'t set session handler');
} 

session_set_save_handler("_open", "_close", "_read", "_write", "_destroy", "_gc");
session_start();

$_SESSION['Username'] = 'clau';  
print_r($_SESSION);?>

因为某些原因在我的if:

        if (session_set_save_handler("_open", "_close", "_read", "_write", "_destroy", "_gc")) {
    die('Works fine');
}
else {
    die('Couldn\'t set session handler');}

它返回了无法设置会话处理程序。     谢谢您的帮助! :d

2 个答案:

答案 0 :(得分:0)

在设置处理程序之前尝试添加session_write_close()函数:

session_write_close();

if (session_set_save_handler("_open", "_close", "_read", "_write", "_destroy", "_gc")) {
    die('Works fine');
} else {
    die('Couldn\'t set session handler');
} 

看起来会话已经开始了。然后重新启动:

session_start();

答案 1 :(得分:0)

首先,这是错误的

if (session_set_save_handler("_open", "_close", "_read", "_write", "_destroy", "_gc")) {
    die('Works fine');
} else {
    die('Couldn\'t set session handler');
}

因为当你打电话给死(...); session_start();不会被解雇而会话无法启动。

其次,你的方法很困惑,试试这个例子:

class session extends SessionHandler { 

        protected $users;
        protected $emailshadow = null; 

        public function setNewUsers(Users $users){


            $this->users = clone $users;

            if(!$this->emailshadow)$this->emailshadow = $users->getShadowEmail();

            return true;

        }

        public function destroy($sessID) { 

            $this->users->setLogout($this->emailshadow);

            return true; 

        } 

        public function gc($sessMaxLifeTime) { 

            $this->users->setLogout($this->emailshadow);

            return true;

        } 
    } 

    $usersession = new session(); 


    session_set_save_handler($usersession,true);

    session_name("op-session");
    session_start();

好的,在这个例子中,我使用由我编码的Users类供用户登录,在这种情况下是

session_destroy();

解雇了它设置用户注销,但最重要的是当您解除注销时写下:

$_SESSION = array();
session_destroy();


if (isset($_SERVER['HTTP_COOKIE'])) {

    $cookies = explode(';', $_SERVER['HTTP_COOKIE']);
     foreach($cookies as $cookie) {

       $parts = explode('=', $cookie);
       $name = trim($parts[0]);

       setcookie($name, '', -(time()+3600*24));
       setcookie($name, '', -(time()+3600*24), '/');

    }

}

如果你没有删除所有cookie,那么这个会话仍然存在。 这对我来说很好,我希望这可以帮助你,并原谅我的英语。