Changeset 42 for trunk/lib/DBSessionHandler.inc.php
- Timestamp:
- Dec 18, 2005 12:16:03 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/DBSessionHandler.inc.php
r41 r42 9 9 10 10 class DBSessionHandler { 11 11 12 12 var $db; // DB object. 13 13 … … 20 20 'create_table' => true, // Automatically create table and verify columns. Better set to false after site launch. 21 21 ); 22 22 23 23 /** 24 24 * Constructor 25 25 * 26 26 * @access public 27 * @param 28 * @return 27 * @param 28 * @return 29 29 * @author Quinn Comendant <quinn@strangecode.com> 30 30 * @since 18 Jul 2005 11:02:50 … … 33 33 { 34 34 $this->_params = array_merge($this->_params, $params); 35 35 36 36 if (isset($db)) { 37 37 if (is_a($db, 'DB')) { 38 38 if ($db->isConnected()) { 39 39 // Use existing db connection. 40 40 $this->db =& $db; 41 41 } else { 42 42 App::logMsg(sprintf('Provided DB object is not connected. %s', mysql_error($db->dbh)), LOG_ERR, __FILE__, __LINE__); 43 43 } 44 44 } else { 45 45 App::logMsg(sprintf('Provided DB object is not valid. %s', gettype($db)), LOG_ERR, __FILE__, __LINE__); … … 48 48 // Create our own new db connection. 49 49 require_once dirname(__FILE__) . '/DB.inc.php'; 50 50 51 51 $this->db =& new DB(); 52 52 $this->db->setParam(array( … … 59 59 'db_die_on_failure' => $this->_params['db_die_on_failure'], 60 60 )); 61 61 62 62 // Connect to database. 63 63 $this->db->connect(); … … 67 67 trigger_error('Invalid DB object or unable to connect to database.', E_USER_ERROR); 68 68 } 69 69 70 70 // Get create tables config from global context. 71 71 if (!is_null(App::getParam('db_create_tables'))) { 72 72 $this->_params['create_table'] = App::getParam('db_create_tables'); 73 73 } 74 74 75 75 // Ensure db table is fit. 76 76 $this->initDB(); 77 77 78 78 ini_set('session.save_handler', 'user'); 79 79 session_set_save_handler( … … 97 97 { 98 98 static $_db_tested = false; 99 99 100 100 if ($recreate_db || !$_db_tested && $this->_params['create_table']) { 101 101 if ($recreate_db) { … … 110 110 KEY last_access (last_access) 111 111 )"); 112 112 113 113 if (!$this->db->columnExists($this->_params['db_table'], array('session_id', 'session_data', 'last_access'))) { 114 114 App::logMsg(sprintf('Database table %s has invalid columns. Please update this table manually.', $this->_params['db_table']), LOG_ALERT, __FILE__, __LINE__); 115 115 trigger_error(sprintf('Database table %s has invalid columns. Please update this table manually.', $this->_params['db_table']), E_USER_ERROR); 116 116 } 117 } 117 } 118 118 $_db_tested = true; 119 119 } … … 121 121 function dbSessionOpen($save_path, $sess_name) 122 122 { 123 return true; 123 return true; 124 124 } 125 125 126 126 function dbSessionClose() 127 127 { 128 128 return true; 129 129 } 130 130 131 131 function dbSessionRead($session_id) 132 132 { 133 // Select the data belonging to session $session_id from the session table 133 // Select the data belonging to session $session_id from the session table 134 134 $qid = $this->db->query("SELECT session_data FROM " . $this->_params['db_table'] . " WHERE session_id = '" . addslashes($session_id) . "'"); 135 135 136 136 // Return the session data that was found 137 137 if (mysql_num_rows($qid) == 1) { … … 139 139 return $row[0]; 140 140 } 141 141 142 142 // NOTICE: Output is expected to be an empty string always rather than 'false'. 143 143 return ''; 144 144 } 145 145 146 146 function dbSessionWrite($session_id, $session_data) 147 { 147 { 148 148 // Write the serialized session data ($session_data) to the session table 149 149 $this->db->query("REPLACE INTO " . $this->_params['db_table'] . "(session_id, session_data, last_access) VALUES ('" . addslashes($session_id) . "', '" . addslashes($session_data) . "', null)"); 150 151 return true; 150 151 return true; 152 152 } 153 153 154 154 function dbSessionDestroy($session_id) 155 155 { 156 156 // Delete from the table all data for the session $session_id 157 157 $this->db->query("DELETE FROM " . $this->_params['db_table'] . " WHERE session_id = '" . addslashes($session_id) . "'"); 158 159 return true; 158 159 return true; 160 160 } 161 161 162 162 function dbSessionGarbage($max_lifetime=4000) 163 163 { 164 164 // Delete old values from the session table 165 165 $qid = $this->db->query("DELETE FROM " . $this->_params['db_table'] . " WHERE UNIX_TIMESTAMP(last_access) < " . (time() - $max_lifetime)); 166 167 return true; 166 167 return true; 168 168 } 169 169 }
Note: See TracChangeset
for help on using the changeset viewer.