Walter Ebert avatar Walter Ebert committed 7fd63a2

make table name changable + clean up comments

Comments (0)

Files changed (1)

  * 
  * Requires PHP 5.2 or later
  * 
- * @author    Walter Ebert (http://www.walterebert.com)
- * @copyright Copyright (c) 2008 Walter Ebert
- * @license   http://www.opensource.org/licenses/mit-license.php MIT License
- * @version   0.9
- * @link      http://www.walterebert.com/code/session-pdo.html
+ * @author      Walter Ebert (http://walterebert.com/)
+ * @license    http://www.opensource.org/licenses/mit-license.php MIT License
+ * @link         https://bitbucket.org/walterebert/session-pdo
  */
 class we_sessionPdo
 {
     /**
-     * Database connection
-     * 
-     * @access protected
-     * @var    object
-     */
-    protected $db = null;
+       * Database connection
+       * 
+       * @var object
+       */
+    protected $db;
 
     /**
-     * Use database transaction
-     * 
-     * @access protected
-     * @var    boolean
-     */
+       * Database table for session data
+       *
+       * @var string
+       */
+    protected $dbTable;
+
+    /**
+       * Use database transaction
+       * 
+       * @var    boolean
+       */
     protected $transaction = false;
 
     /**
-     * Regenerate session ID
-     * 
-     * @access protected
-     * @var    boolean
-     */
+       * Regenerate session ID
+       * 
+       * @var    boolean
+       */
     protected $regenerate_id = false;
 
     /**
-     * Constructor
-     *
-     * @access public
-     * @param  object  $pdo         PDO database object
-     * @param  boolean $transaction Use database transactions [optional]
-     * @param  boolean $sessionName Session name [optional]
-     * @return void
-     */
-    public function __construct($pdo, $transaction = false, $sessionName = 'PDOSESSID')
+       * Constructor
+       *
+       * @param  object  $pdo PDO database object
+       * @param  boolean $transaction Use database transactions [optional]
+       * @param  boolean $sessionName Session name [optional]
+       * @param  string $dbTable Database table [optional]
+       * @return void
+       */     
+    public function __construct(PDO $pdo, $transaction = true, $sessionName = 'PDOSESSID', $dbTable = 'session_data')
     {
+        // Set database table name
+        $this->dbTable = (string) $dbTable;
+        
         // Set database connection
         $this->db = $pdo;
         if( $transaction ) {
     }
 
     /**
-     * Regenerate session ID after the session call.
-     *
-     * @access public
-     * @return void
-     */
+       * Regenerate session ID after the session call.
+       *
+       * @return void
+       */
     public function regenerate_id()
     {
         $this->regenerate_id = true;
     }
 
     /**
-     * Destroy session, session data and session cookie.
-     * 
-     * @access public
-     * @return void
-     */
+       * Destroy session, session data and session cookie.
+       * 
+       * @return void
+       */
     public function destroy()
     {
         $_SESSION = array();
     }
 
     /**
-     * Get raw session data from database.
-     *
-     * @access protected
-     * @param  string $id Session ID
-     * @return array or false
-     */
+       * Get raw session data from database.
+       *
+       * @param  string $id Session ID
+       * @return array or false
+       */
     protected function _fetchSession($id)
     {
-        $stmt = $this->db->prepare('SELECT id, data FROM session_data WHERE id = :id AND unixtime > :unixtime');
+        $stmt = $this->db->prepare('SELECT id, data FROM ' . $this->_getTable() . ' WHERE id = :id AND unixtime > :unixtime');
         $stmt->execute( array(':id' => $id, ':unixtime' => ( time() - (int) ini_get('session.gc_maxlifetime') ) ) );
         $sessions = $stmt->fetchAll();
 
+        if( $this->transaction ) {
+            $this->db->commit();
+	}
+
         return empty($sessions) ? false : $sessions[0] ;
     }
 
     /**
-     * Open session. Not relevant to this class.
-     *
-     * @access protected
-     * @param  $savePath
-     * @param  $sessionName
-     * @return true
-     */
+       *  Get database table name for session data
+       * 
+       * @return string
+       */
+    protected function _getTable()
+    {
+        $table = $this->db->quote($this->dbTable);
+
+        // If database driver does not support quoting return raw string
+        if( !$table ) {
+            $table = $this->dbTable;
+        }
+        
+        return $table;
+    }
+    
+    /**
+       * Open session. Not relevant to this class.
+       *
+       * @param  $savePath
+       * @param  $sessionName
+       * @return true
+       */
     protected function _open($savePath, $sessionName)
     {
         return true;
     }
 
     /**
-     * Close session. Not relevant to this class.
-     *
-     * @access protected
-     * @return true
-     */
+       * Close session. Not relevant to this class.
+       *
+       * @return true
+       */
     protected function _close()
     {
         return true;
     }
 
     /**
-     * Read session data.
-     *
-     * @access protected
-     * @param  string $id Session ID
-     * @return string or false
-     */
+       * Read session data.
+       *
+       * @param  string $id Session ID
+       * @return string or false
+       */
     protected function _read($id)
     {
         $session = $this->_fetchSession($id);
 
-        return ( $session == false ) ? false : $session['data'] ;
+        return ( $session === false ) ? false : $session['data'] ;
     }
 
     /**
-     * Write session data.
-     *
-     * @access protected
-     * @param  string $id          Session ID
-     * @param  string $sessionData Session data
-     * @return void
-     */
+       * Write session data.
+       *
+       * @param  string $id Session ID
+       * @param  string $sessionData Session data
+       * @return void
+       */
     protected function _write($id, $sessionData)
     {
         $session = $this->_fetchSession($id);
-        if( $session == false ) {
-            $stmt = $this->db->prepare('INSERT INTO session_data (id, data, unixtime) VALUES (:id, :data, :time)');
+        if( $session === false ) {
+            $stmt = $this->db->prepare('INSERT INTO ' . $this->_getTable() . ' (id, data, unixtime) VALUES (:id, :data, :time)');
         } else {
-            $stmt = $this->db->prepare('UPDATE session_data SET data = :data, unixtime = :time WHERE id = :id');
+            $stmt = $this->db->prepare('UPDATE ' . $this->_getTable() . ' SET data = :data, unixtime = :time WHERE id = :id');
         }
-        $stmt->execute(array(':id' => $id, ':data' => $sessionData, ':time' => time()));
+	$stmt->execute( array(
+            ':id' => $id,
+            ':data' => $sessionData,
+            ':time' => time()
+        ));
+
+	if( $this->transaction ) {
+	    $this->db->commit();
+	}
     }
 
     /**
-     * Destroy session.
-     *
-     * @access protected
-     * @param  string $id Session ID
-     * @return void
-     */
+       * Destroy session
+       *
+       * @param  string $id Session ID
+       * @return void
+       */
     protected function _destroy($id)
     {
-        $stmt = $this->db->prepare('DELETE FROM session_data WHERE id = :id');
+        $stmt = $this->db->prepare('DELETE FROM ' . $this->_getTable() . ' WHERE id = :id');
         $stmt->execute(array(':id' => $id));
     }
 
     /**
-     * Garbage collection.
-     *
-     * @access protected
-     * @param  integer $maxlifetime Maximum session life time
-     * @return void
-     */
+       * Garbage collection.
+       *
+       * @param  integer $maxlifetime Maximum session life time
+       * @return void
+       */
     protected function _gc($maxlifetime)
     {
-        $stmt = $this->db->prepare('DELETE FROM session_data WHERE unixtime < :time');
+        $stmt = $this->db->prepare('DELETE FROM ' . $this->_getTable() . ' WHERE unixtime < :time');
         $stmt->execute(array( ':time' => (time() - (int) $maxlifetime) ));
     }
 
     /**
-     * Destructor
-     *
-     * @access public 
-     * @return void
-     */
+       * Destructor
+       *
+       * @return void
+       */
     public function __destruct()
     {
         // Create new session ID
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.