source: trunk/lib/DBSessionHandler.inc.php @ 201

Last change on this file since 201 was 201, checked in by scdev, 18 years ago

Q - increased some LOG_DEBUG messages to LOG_INFO so we can run with debugging off and still get the important ones.

File size: 5.3 KB
Line 
1<?php
2/**
3 * DBSessionHandler.inc.php
4 * Code by Strangecode :: www.strangecode.com :: This document contains copyrighted information.
5 *
6 * @author  Quinn Comendant <quinn@strangecode.com>
7 * @version 2.1
8 * @since   1999
9 */
10
11class DBSessionHandler {
12
13    var $db; // DB object.
14
15    var $_params = array(
16        'db_table' => 'session_tbl',
17
18        // Automatically create table and verify columns. Better set to false after site launch.
19        'create_table' => true,
20    );
21
22    /**
23     * Constructor
24     *
25     * @access  public
26     * @param
27     * @return
28     * @author  Quinn Comendant <quinn@strangecode.com>
29     * @since   18 Jul 2005 11:02:50
30     */
31    function DBSessionHandler($db, $params=array())
32    {
33        $app =& App::getInstance();
34   
35        $this->_params = array_merge($this->_params, $params);
36
37        if (!method_exists($db, 'isConnected')) {
38            $app->logMsg(sprintf('Provided object (%s) is not a valid DB object.', get_class($db)), LOG_ERR, __FILE__, __LINE__);
39        } else {
40            if (!$db->isConnected()) {
41                $app->logMsg('Provided DB object is not connected.', LOG_ERR, __FILE__, __LINE__);
42            } else {
43                // OK! We have a valid, connected DB object.
44                $this->db =& $db;
45
46                // Get create tables config from global context.
47                if (!is_null($app->getParam('db_create_tables'))) {
48                    $this->_params['create_table'] = $app->getParam('db_create_tables');
49                }
50
51                // Ensure db table is fit.
52                $this->initDB();
53
54                ini_set('session.save_handler', 'user');
55                session_set_save_handler(
56                    array(&$this, 'dbSessionOpen'),
57                    array(&$this, 'dbSessionClose'),
58                    array(&$this, 'dbSessionRead'),
59                    array(&$this, 'dbSessionWrite'),
60                    array(&$this, 'dbSessionDestroy'),
61                    array(&$this, 'dbSessionGarbage')
62                );
63            }
64        }
65    }
66
67    /**
68     * Setup the database table for this class.
69     *
70     * @access  public
71     * @author  Quinn Comendant <quinn@strangecode.com>
72     * @since   26 Aug 2005 17:09:36
73     */
74    function initDB($recreate_db=false)
75    {
76        $app =& App::getInstance();
77   
78        static $_db_tested = false;
79
80        if ($recreate_db || !$_db_tested && $this->_params['create_table']) {
81            if ($recreate_db) {
82                $this->db->query("DROP TABLE IF EXISTS " . $this->db->escapeString($this->_params['db_table']));
83                $app->logMsg(sprintf('Dropping and recreating table %s.', $this->_params['db_table']), LOG_INFO, __FILE__, __LINE__);
84            }
85            $this->db->query("CREATE TABLE IF NOT EXISTS " . $this->db->escapeString($this->_params['db_table']) . " (
86                session_id char(32) NOT NULL default '',
87                session_data mediumtext NOT NULL,
88                last_access timestamp(14) NOT NULL,
89                PRIMARY KEY (session_id),
90                KEY last_access (last_access)
91            )");
92
93            if (!$this->db->columnExists($this->_params['db_table'], array('session_id', 'session_data', 'last_access'))) {
94                $app->logMsg(sprintf('Database table %s has invalid columns. Please update this table manually.', $this->_params['db_table']), LOG_ALERT, __FILE__, __LINE__);
95                trigger_error(sprintf('Database table %s has invalid columns. Please update this table manually.', $this->_params['db_table']), E_USER_ERROR);
96            }
97        }
98        $_db_tested = true;
99    }
100
101    function dbSessionOpen($save_path, $sess_name)
102    {
103        return true;
104    }
105
106    function dbSessionClose()
107    {
108        return true;
109    }
110
111    function dbSessionRead($session_id)
112    {
113        // Select the data belonging to session $session_id from the session table
114        $qid = $this->db->query("SELECT session_data FROM " . $this->db->escapeString($this->_params['db_table']) . " WHERE session_id = '" . $this->db->escapeString($session_id) . "'");
115
116        // Return the session data that was found
117        if (mysql_num_rows($qid) == 1) {
118            $row = mysql_fetch_row($qid);
119            return $row[0];
120        }
121
122        // NOTICE: Output is expected to be an empty string always rather than 'false'.
123        return '';
124    }
125
126    function dbSessionWrite($session_id, $session_data)
127    {
128        // Write the serialized session data ($session_data) to the session table
129        $this->db->query("REPLACE INTO " . $this->db->escapeString($this->_params['db_table']) . "(session_id, session_data, last_access) VALUES ('" . $this->db->escapeString($session_id) . "', '" . $this->db->escapeString($session_data) . "', null)");
130
131        return true;
132    }
133
134    function dbSessionDestroy($session_id)
135    {
136        // Delete from the table all data for the session $session_id
137        $this->db->query("DELETE FROM " . $this->db->escapeString($this->_params['db_table']) . " WHERE session_id = '" . $this->db->escapeString($session_id) . "'");
138
139        return true;
140    }
141
142    function dbSessionGarbage($max_lifetime=4000)
143    {
144        // Delete old values from the session table
145        $qid = $this->db->query("DELETE FROM " . $this->db->escapeString($this->_params['db_table']) . " WHERE UNIX_TIMESTAMP(last_access) < " . (time() - $max_lifetime));
146
147        return true;
148    }
149}
150
151?>
Note: See TracBrowser for help on using the repository browser.