PHP自定义会话处理程序

时间:2016-01-10 10:41:38

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

我在自定义会话类中使用session_set_save_handler来使用数据库存储。我使用数据库会话存储已超过一年。这一直都很好,我写的验证码也差不多一年前。我现在遇到的问题是验证码不再输出图像。如果我将require_once移到会话类中,则会输出图像,但代码会像使用session_start一样保存,而不是使用自定义类。如果我需要会话类,则不显示图像,但代码按预期编写。我使用相同的代码差不多一年没有问题。我完全不知道发生了什么。

<?php

require_once('database.php');
require_once('config.php');

class FileSessionHandler
{

private $database;
private $life_time;

public function FileSessionHandler(){

$this->life_time = get_cfg_var("session.gc_maxlifetime");
$this->database = new database();
$this->database->newConnection        (db_host,db_users_name,db_users_pass,db_users_prefix . db_users);

session_set_save_handler(
array(&$this,'open'),
array(&$this,'close'),
array(&$this,'read'),
array(&$this,'write'),
array(&$this,'destroy'),
array(&$this,'gc')
);

register_shutdown_function('session_write_close');
session_start();

}


function open($savePath,$sessionName){
    return TRUE;
}
function close(){
    return TRUE;
}


function read($id){
    $data = "";
    $time = time();
    $newid =  $this->database->sanitizeData($id);
    $sql = "SELECT `session_data` FROM `" . tb_site_sessions . "` 
            WHERE `session_id`='{$newid}' AND `expires`>{$time}";

    $this->database->executeQuery($sql);    

    if($this->database->numRows() > 0) {
        $row = $this->database->getRows();
        $data = $row['session_data'];
    }

    return $data;
}


function write($id,$data){

$newid = $this->database->sanitizeData($id);
$newdata = $this->database->sanitizeData($data);
$time = time() + $this->life_time;

$sql = "INSERT INTO `" . tb_site_sessions . "`    
              (`session_id`,`session_data`,`expires`) 
        VALUES('{$newid}','{$newdata}',{$time}) ON DUPLICATE KEY UPDATE 
             session_data='{$newdata}',expires={$time}";

$this->database->executeQuery($sql);

return TRUE;

 }


function destroy($id){

$newid = $this->database->sanitizeData($id);
$sql = "DELETE FROM `" . tb_site_sessions . "` WHERE `session_id`='{$newid}'";
$this->database->executeQuery($sql);

return TRUE;

}


function gc($maxlifetime){
$time = time();

$sql = "DELETE FROM `" . tb_site_sessions . "` WHERE `expires`<'{$time}'";
$this->database->executeQuery($sql);

return TRUE;
}

}



?>

致电方法:

require_once('site-sessions.php');
$handler = new FileSessionHandler();

图片标题:

 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
 header("Cache-Control: no-store, no-cache, must-revalidate");
 header("Cache-Control: post-check=0, pre-check=0", false);
 header("Pragma: no-cache");

1 个答案:

答案 0 :(得分:0)

发现问题并且很简单。在配置文件中,php标记前面有一个空行。空白行正在弄乱标题和会话调用。我甚至没有考虑查看配置文件,因为它在一段时间内没有被改变。只是包括答案,万一其他人遇到类似的情况。

相关问题