簡體   English   中英

自定義數據庫會話處理程序-特定的讀取問題

[英]custom database session handler - specific read issue

關於使用mysql數據庫的自定義會話處理程序,我有一個非常具體的問題。 我瀏覽了其他主題,因為這里有一些涉及自定義會話處理程序的主題,但是沒有人回答我的問題。 我在名為“會話”的類中使用了自定義會話處理程序:

require_once "mySQL.php";

class sessions {
private $db;
public function __construct(){
    $this->db=new mySQL('localhost', user, pw, database);
    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();
} 
public function read($sessionID){
    $abfrage="SELECT * FROM sessions WHERE id='".$sessionID."'";
    $result= $this->db->query($abfrage);
    if ($result == false){
        return '';
    }
    if (count($result) > 0){
        return $result[0]["value"];
    }
    else{ 
    return '';
    }
}

在“ mySQL.php”中,僅編寫了一個類“ mySQL”,用於創建與數據庫的mysqli連接。 這是可行的,而不是問題。 我想將重點放在“讀取”例程上,因為這似乎是主要問題。 如果我現在通過創建會話

require_once "classes/sessions.php";
$SESSION=new sessions();
$token=hash("sha256",session_id().time());
$_SESSION['token']=$token;
$_SESSION['running']=1

並將值寫入創建正確數據庫值的會話中:

id (varchar 64) = 54ul84nhc3aimqc55efcs49js4
lastUpdated (int 11) = 1352226239
start (int 11) = 1352226233
value (varchar 20000) =     token|s:64:"2b92aa848a86741ef11d800c3dce527fdcee264f50de10381941241c3d9cc9ee";running|i:1;

甚至垃圾收集器也能正常工作,但是當我重新加載頁面並嘗試回顯時

$_SESSION['running']

我收到錯誤消息“ Notice:Undefined index:running”。 調試器運行告知上述發布的讀取例程返回1(等於布爾值“ true” ?!),而不是該值。 我檢查了在這種情況下讀取例程中的“ sessionID”是否為會話表中的那個,所以我基本上迷路了。 如果您能幫助我,那將非常好,因為到目前為止,我已經對這種自定義會話處理程序所提供的可能性印象深刻。

也許有可能在這里提出一個附帶問題。 是的,當我重新加載頁面時,會話的構造函數由“ $ SESSION = newsession();”調用。 即使已經有一個會話正在運行(但是此行為不會更改session_id)? 還是這是由於我的會話讀取例程中斷了?

謝謝您的幫助。

編輯:我在xampp發行版中使用PHP / 5.4.4和MySQL 5.5.25a以及phpmyAdmin 3.5.2來建立數據庫。 會話表是“ MEMORY”格式的表,這就是為什么我使用varchar 20000字段而不是文本字段的原因。 我剛剛看過這篇文章, 為什么mysql_query()用SELECT語句返回TRUE? 指出當服務器突然中斷連接時,mysql_query不時返回true。 但是我不能弄清楚這可能是什么原因。 我現在添加了mySQL類,可能是此問題涉及錯誤:

<?php
class mySQL
{
public $mySQLiObj;
public $lastSQLQuery;
public $lastSQLStatus;
public $lastInsertID;
public function __construct($server,$user,$password,$db,$port="3306"){
    //erstelle db-objekt
    $this->mySQLiObj= new mysqli($server,$user,$password,$db,$port);
    if (mysqli_connect_errno()){
        echo "Keine Verbindung zum MySQL-Server möglich.";
        trigger_error("MYSQL-Connection-Error",E_USER_ERROR);           
        die();
    }
    //Zeichensatz auf utf8 setzen
    $this->query("SET NAMES utf8");
}
public function __destruct(){
    $this->mySQLiObj->close();
}
public function query($sqlQuery,$resultset=false){
    $this->lastSQLQuery=$sqlQuery;
    $result=$this->mySQLiObj->query($sqlQuery);
    if($resultset == true){
        if($result == false) {
            $this->lastSQLStatus=false;
        }
        else
        {
            $this->lastSQLStatus = true;
        }
        return $result;
    }
    if($resultset == false) {
        $return = $this->makeArrayResult($result);
        return $return;
    }
}
private function makeArrayResult($ResultObj){
    if ($ResultObj==false){
        //Fehler aufgetreten
        $this->lastSQLStatus=false;
        return false;
    }
    else if ($ResultObj == true) {
        //UPDATE,INSERT etc. statement es wird nur true zurückgegeben
        $this->lastSQLStatus = true;
        $this->lastInsertID = $this->mySQLiObj->insert_id;
        return true;
    }
    else if ($ResultObj->num_rows == 0){
        //Kein Ergebnis bei SELECT,SHOW,DESCRIBE oder EXPLAIN Statement
        $this->lastSQLStatus = true;
        return array();
    }
    else {
        $array = array();
        while($line = $ResultObj->fetch_array(MYSQLI_ASSOC)){
            array_push($array,$line);
        }
        $this->lastSQLStatus=true;
        return $array;
    }
}

}

因此,經過大量的嘗試和錯誤,然后回頭看了一本書,從中我得到了會話處理程序的基本布局,我自己回答了這個問題。 好像是這樣

$SESSION=new sessions();

是關鍵點。 不必通過顯式調用來創建會話,而必須調用

session_set_save_handler(new sessions);

並從session類的構造函數中刪除該行。 然而,有趣的是,這種奇怪的錯誤由此而產生。

感謝您的關注,也許這會對某人有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM