1+ <?php
2+
3+ require_once 'session_handler_interface.php ' ;
4+
5+ /**
6+ * Database Session Hander
7+ *
8+ * @author Brett Millett <bmillett@olwm.com>
9+ * @version 1.0
10+ */
11+ class SessionHandler implements SessionHandlerInterface {
12+
13+ public $ table = 'session_handler ' ;
14+
15+ protected $ dbh = NULL ;
16+ protected $ session_id = NULL ;
17+ protected $ session_written = FALSE ;
18+
19+ const ADMIN_EMAIL = 'it@olwm.com ' ;
20+
21+ /**
22+ * Automatically sets this instance to database session handler.
23+ *
24+ * @param PDO $db A PDO instance.
25+ */
26+ public function __construct (PDO $ db ) {
27+ $ this ->dbh = & $ db ;
28+
29+ // Register this object as the session handler
30+ session_set_save_handler (
31+ array (&$ this , 'open ' ), array (&$ this , 'close ' ),
32+ array (&$ this , 'read ' ), array (&$ this , 'write ' ),
33+ array (&$ this , 'destroy ' ), array (&$ this , 'gc ' )
34+ );
35+
36+ // the following prevents unexpected effects when using objects as save handlers
37+ register_shutdown_function ('session_write_close ' );
38+
39+ session_start ();
40+ }
41+
42+ /**
43+ * @return boolean
44+ */
45+ public function close () {
46+ /**
47+ * Keep session alive with db update where we will call this each method
48+ * each time via session_write_close. This may not be neccessary.
49+ */
50+ if (!empty ($ this ->session_id ) && !$ this ->session_written ) {
51+ try {
52+ $ stmt = $ this ->prepare ('UPDATE `%s` SET `timestamp` = NOW() WHERE `id` = ? ' );
53+ $ stmt ->execute (array ($ session_id ));
54+ } catch (PDOException $ e ) {
55+ $ this ->email_admins ($ e ->getMessage ());
56+ } catch (Exception $ e ) {
57+ $ this ->email_admins ($ e ->getMessage ());
58+ }
59+ }
60+ return TRUE ;
61+ }
62+
63+ /**
64+ *
65+ * @param string $session_id
66+ * @return boolean
67+ */
68+ public function destroy ($ session_id ) {
69+ try {
70+ $ stmt = $ this ->prepare ('DELETE FROM `%s` WHERE `id` = ? ' );
71+ $ stmt ->execute (array ($ session_id ));
72+ $ destroyed = ($ stmt ->rowCount () > 0 );
73+ if ($ destroyed )
74+ $ this ->session_id = NULL ;
75+ return $ destroyed ;
76+ } catch (PDOException $ e ) {
77+ $ this ->email_admins ($ e ->getMessage ());
78+ } catch (Exception $ e ) {
79+ $ this ->email_admins ($ e ->getMessage ());
80+ }
81+ return FALSE ;
82+ }
83+
84+ /**
85+ *
86+ * @param string $maxlifetime
87+ * @return boolean
88+ */
89+ public function gc ($ maxlifetime ) {
90+ try {
91+ $ stmt = $ this ->prepare ('DELETE FROM `%s` WHERE `timestamp` < ? ' );
92+ $ stmt ->execute (array (time () - intval ($ maxlifetime )));
93+ return ($ stmt ->rowCount () > 0 );
94+ } catch (PDOException $ e ) {
95+ $ this ->email_admins ($ e ->getMessage ());
96+ } catch (Exception $ e ) {
97+ $ this ->email_admins ($ e ->getMessage ());
98+ }
99+ return FALSE ;
100+ }
101+
102+ /**
103+ *
104+ * @param string $save_path
105+ * @param string $name
106+ * @return boolean
107+ */
108+ public function open ($ save_path , $ name ) {
109+ if ($ this ->dbh instanceof PDO )
110+ return TRUE ;
111+ return FALSE ;
112+ }
113+
114+ /**
115+ *
116+ * @param string $session_id
117+ * @return string
118+ */
119+ public function read ($ session_id ) {
120+ $ this ->session_id = $ session_id ;
121+ try {
122+ $ stmt = $ this ->prepare ('SELECT `data` FROM `%s` WHERE id = ? ' );
123+ $ stmt ->execute (array ($ session_id ));
124+ $ result = $ stmt ->fetch (PDO ::FETCH_OBJ );
125+ return (empty ($ result )) ? '' : $ result ->data ;
126+ } catch (PDOException $ e ) {
127+ $ this ->email_admins ($ e ->getMessage ());
128+ } catch (Exception $ e ) {
129+ $ this ->email_admins ($ e ->getMessage ());
130+ }
131+ return '' ;
132+ }
133+
134+ /**
135+ *
136+ * @param string $session_id
137+ * @param string $session_data
138+ * @param integer $timestamp
139+ * @return boolean
140+ */
141+ public function write ($ session_id , $ session_data , $ timestamp = 0 ) {
142+ $ this ->session_written = TRUE ;
143+ try {
144+ $ stmt = $ this ->prepare ('REPLACE INTO `%s` VALUES(?, ?, ?) ' );
145+ $ stmt ->execute (array ($ session_id , $ session_data , ((int ) $ timestamp > 0 ) ? (int ) $ timestamp : time ()));
146+ return ($ stmt ->rowCount () > 0 );
147+ } catch (PDOException $ e ) {
148+ $ this ->email_admins ($ e ->getMessage ());
149+ } catch (Exception $ e ) {
150+ $ this ->email_admins ($ e ->getMessage ());
151+ }
152+ return FALSE ;
153+ }
154+
155+ protected function email_admins ($ message ) {
156+ mail (self ::ADMIN_EMAIL , __CLASS__ . ' Error ' , $ message );
157+ }
158+
159+ protected function prepare ($ query ) {
160+ return $ this ->dbh ->prepare (sprintf ($ query , $ this ->table ));
161+ }
162+
163+ }
164+
165+ //class
166+
167+ /** PHP MySQL Session Handler
168+ ========================
169+
170+ Installation
171+ ----------------------------
172+
173+ First you need to create a table in your database:
174+
175+ CREATE TABLE `session_handler` (
176+ `id` varchar(255) NOT NULL,
177+ `data` mediumtext NOT NULL,
178+ `timestamp` int(255) NOT NULL,
179+ PRIMARY KEY (`id`)
180+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
181+ *
182+ */
0 commit comments