2010-06-02 54 views
0

我想保存我在一個數據庫中的所有會話,並在此閱讀並實施以下類:在DB存儲會話中的PHP - 不斷得到MySQL錯誤

<?php 
/** 
* This class handles users sessions and stores the session in the DB rather than in a file. This way stops any 
* shared host security problems that could potentially happen. 
*/ 
class sessionHandler { 

    /** 
    * Initial constructor which takes the database object as a param, to do all the database stuff 
    * @param object $db The datase object 
    */ 
    public function __construct ($db) { 
     $this->db = $db; 
     $this->setHandler();  
    } 

    function setHandler() { 

     session_set_save_handler(array(&$this, "open"), 
           array(&$this, "close"), 
           array(&$this, "read"), 
           array(&$this, "write"), 
           array(&$this, "destroy"), 
           array(&$this, "clean") 
           );   
    } 

    /** 
    * Initiate a database object if necessary 
    */ 
    function open() { 
     $this->db->connect(); 
    } 

    /** 
    * Write session id and data to the database 
    * @param string $id The hashed 32 char session id, unique to a user 
    * @param string $data Serialized session array from the unique session 
    * @return id The newly inserted ID of the database 
    */ 
    function write($id, $data) { 
     $access = time(); 
     $dateAdded = date("Y-m-d G:i:s"); 

     $this->db->wrapper->where(array("sessionId"=>$id)); 
     $this->db->query($this->db->wrapper->delete(__CLASS__)); 

     //fopen a file and store this in it that way we can debug 
     $query = $this->db->wrapper->insert(__CLASS__, array("sessionId"=>$id,"dateAdded"=>$dateAdded,"sessionData"=>$data)); 

     $this->db->query($query); 
     return $this->db->insertId(); 
    } 

    /** 
    * Retrieve the session data for a given session id 
    * @param string $id The hashed 32 char session id, unique to a user 
    * @return string The session data found for the given session id 
    */ 
    function read($id) { 
     $id = $this->db->wrapper->escape($id); 
     $row = $this->db->fetch(1, $this->db->wrapper->get_where(__CLASS__,array("sessionId"=>$id)), array(),false); 
     if ($row) { 
      return $row['data'];  
     } 
     return ""; 
    } 

    /** 
    * Delete a session from the database by its unique session id 
    * @param string $id The hashed 32 char session id, unique to a user 
    * @return integer The number of deleted rows - should only ever be 1 
    */ 
    function destroy($id) { 
     $id = $this->db->wrapper->escape($id); 
     $this->db->wrapper->where(array("sessionId"=>$id)); 
     $this->db->query($this->db->wrapper->delete(__CLASS__)); 
     return $this->db->affectedRows(); 
    } 

    /** 
    * Garage collector which deletes old records in the database, delete sessions that have expired. This is 
    * determined by the session.gc_maxlifetime variable in the php.ini 
    * @param integer $max The maximum number of seconds allowed before a session is to be considered expired 
    * @return integer The number of deleted rows 
    */ 
    function clean($max) { 
     $old = time() - $max; 
     $old = $this->db->wrapper->escape($old); 
     $this->db->wrapper->where(array("access"=>$old), "<"); 
     $this->db->query($this->db->wrapper->delete(__CLASS__)); 
     return $this->db->affectedRows(); 

    } 

    /** 
    * Close the database connection once a read/write has been complete 
    */ 
    function close() { 
     $this->db->close(); 
    } 

    /** 
    * End the current session and store session data. 
    */ 
    public function __destruct(){ 
     session_write_close(); 
    }  
} 

正如你可以看到我的代碼,我將DB對象作爲參數傳遞給類。我的引導文件如下:

$db = mogDB::init(); 
    $sh = new sessionHandler($db); 

    session_start(); 

我使用了一些我自己的類此這樣MogDB:的init()基本上創建一個具有適當憑據的數據庫連接。包裝的東西基本上是這樣的,我不必在sql查詢之後輸入sql查詢(我有點懶惰,我猜)。

,但我得到的問題是這在我的PHP錯誤日誌:

08-Apr-2010 17:40:31] PHP Warning: mysql_insert_id(): 11 is not a valid MySQL-Link resource in /library/mysql.php on line 69 

我已經調試這是盡我所能,似乎當它試圖寫該會話的DB,它是失敗。我設法將查詢保存到一個文件,並通過phpmyadmin導入到數據庫罰款,所以它不是查詢這是問題。

線69 mysql.php如下:

68. public function insertId() { 
69.  return mysql_insert_id($this->id); 
70. } 

任何幫助將不勝感激 感謝

+0

哦,親愛的主,你正在運行一個MySQL每次請求會話值時每次查詢都會查詢?哇,只是哇。 – animuson 2010-06-02 22:41:46

+0

請在第69行中包含文件/library/mysql.php的內容,以便我們看到發生了什麼。這是報告的錯誤。 – Zak 2010-06-02 22:43:29

+0

@animuson有沒有更好的方法?使用'session_set_save_handler()'似乎暗示它每次都在執行讀/寫操作。唯一的區別是內置的使用文件而不是數據庫。 – 2010-06-02 22:48:34

回答

1

後您包括圍繞線69文件/library/mysql.php的內容所以我們可以看到發生了什麼,我猜測你的代碼的一小部分是要求重新調用mysql_insert_id的數據庫句柄,而不是你的db api已經返回實際的insertID ...

+0

我現在在mysql.php中添加了行68,69和70 – phpNutt 2010-06-02 22:47:44

+0

我還在上面的錯誤 之前收到此錯誤警告:mysql_query():11不是/library/mysql.php中的有效MySQL-Link資源在線43 – phpNutt 2010-06-02 22:50:16

+0

正確的,而不是,只是返回$ this->編號... – Zak 2010-06-02 22:53:57