forked from bretticus/db-sessions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession_handler.php
More file actions
161 lines (144 loc) · 4.6 KB
/
session_handler.php
File metadata and controls
161 lines (144 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
/**
* Database Session Hander
*
* @author Brett Millett <bmillett@olwm.com>
* @version 1.0
*/
class PDOSessionHandler implements SessionHandlerInterface {
public $table = 'session_handler';
protected $dbh = NULL;
protected $session_id = NULL;
protected $session_written = FALSE;
const ADMIN_EMAIL = 'admin@domain.tld';
/**
* Automatically sets this instance to database session handler.
*
* @param PDO $db A PDO instance.
*/
public function __construct(PDO $db) {
$this->dbh = & $db;
// Register this object as the session handler
session_set_save_handler(
array(&$this, 'open'), array(&$this, 'close'),
array(&$this, 'read'), array(&$this, 'write'),
array(&$this, 'destroy'), array(&$this, 'gc')
);
// the following prevents unexpected effects when using objects as save handlers
register_shutdown_function('session_write_close');
session_start();
}
/**
* @return boolean
*/
public function close() {
/**
* Keep session alive with db update where we will call this each method
* each time via session_write_close. This may not be neccessary.
*/
if (!empty($this->session_id) && !$this->session_written) {
try {
$stmt = $this->prepare('UPDATE `%s` SET `timestamp` = NOW() WHERE `id` = ?');
$stmt->execute(array($session_id));
} catch (PDOException $e) {
$this->email_admins($e->getMessage());
} catch (Exception $e) {
$this->email_admins($e->getMessage());
}
}
return TRUE;
}
/**
*
* @param string $session_id
* @return boolean
*/
public function destroy($session_id) {
try {
$stmt = $this->prepare('DELETE FROM `%s` WHERE `id` = ?');
$stmt->execute(array($session_id));
$destroyed = ($stmt->rowCount() > 0);
if ($destroyed)
$this->session_id = NULL;
return $destroyed;
} catch (PDOException $e) {
$this->email_admins($e->getMessage());
} catch (Exception $e) {
$this->email_admins($e->getMessage());
}
return FALSE;
}
/**
*
* @param string $maxlifetime
* @return boolean
*/
public function gc($maxlifetime) {
try {
$stmt = $this->prepare('DELETE FROM `%s` WHERE `timestamp` < ?');
$stmt->execute(array(time() - intval($maxlifetime)));
return ($stmt->rowCount() > 0);
} catch (PDOException $e) {
$this->email_admins($e->getMessage());
} catch (Exception $e) {
$this->email_admins($e->getMessage());
}
return FALSE;
}
/**
*
* @param string $save_path
* @param string $name
* @return boolean
*/
public function open($save_path, $name) {
if ($this->dbh instanceof PDO)
return TRUE;
return FALSE;
}
/**
*
* @param string $session_id
* @return string
*/
public function read($session_id) {
$this->session_id = $session_id;
try {
$stmt = $this->prepare('SELECT `data` FROM `%s` WHERE id = ?');
$stmt->execute(array($session_id));
$result = $stmt->fetch(PDO::FETCH_OBJ);
return (empty($result)) ? '' : $result->data;
} catch (PDOException $e) {
$this->email_admins($e->getMessage());
} catch (Exception $e) {
$this->email_admins($e->getMessage());
}
return '';
}
/**
*
* @param string $session_id
* @param string $session_data
* @param integer $timestamp
* @return boolean
*/
public function write($session_id, $session_data, $timestamp = 0) {
$this->session_written = TRUE;
try {
$stmt = $this->prepare('REPLACE INTO `%s` VALUES(?, ?, ?)');
$stmt->execute(array($session_id, $session_data, ((int) $timestamp > 0) ? (int) $timestamp : time()));
return ($stmt->rowCount() > 0);
} catch (PDOException $e) {
$this->email_admins($e->getMessage());
} catch (Exception $e) {
$this->email_admins($e->getMessage());
}
return FALSE;
}
protected function email_admins($message) {
mail(self::ADMIN_EMAIL, __CLASS__ . ' Error', $message);
}
protected function prepare($query) {
return $this->dbh->prepare(sprintf($query, $this->table));
}
}