在DB存储会话中的PHP - 不断得到MySQL错误

问题描述:

我想保存我在一个数据库中的所有会话,并在此阅读并实施以下类:在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

后您包括围绕线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