-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBSessionHandler.php
More file actions
119 lines (99 loc) · 3.17 KB
/
DBSessionHandler.php
File metadata and controls
119 lines (99 loc) · 3.17 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
<?php
require_once 'debugSettings.php';
require '../dbinfo/dbcred.php';
class DBSessionHandler implements SessionHandlerInterface {
protected $table_sess = 'sessions';
protected $col_sid = 'sid';
protected $col_expiry = 'expiry';
protected $col_data = 'data';
protected $expiry;
protected $db;
public function __construct() {
// Create DB connection
$this->db = $this->dbConnect();
$this->expiry = time() + (int) ini_get('session.gc_maxlifetime') * 60 * 30 * 20;
}
private function dbConnect() {
try {
$db = new PDO('mysql:host=localhost;dbname='.DB_NAME, DB_USERNAME, DB_PASSWORD);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $db;
} catch (PDOException $e) {
//echo $e->getMessage();
return $e->getMessage();
}
}
/**
* Initialize session
* @param string $save_path The path where to store/retrieve the session.
* @param string $name The session name.
* @return bool
*/
public function open($save_path, $name) {
return true;
}
/**
* Reads session data
* @param string $session_id The session id to read data for.
* @return string
*/
public function read($session_id) {
$sql = "SELECT $this->col_expiry, $this->col_data
FROM $this->table_sess WHERE $this->col_sid =" . $this->db->quote($session_id);
$result = $this->db->query($sql);
$data = $result->fetch(PDO::FETCH_ASSOC);
if ($data) {
if ($data[$this->col_expiry] < time()) {
// Return an empty string if data out of date
return '';
}
return $data[$this->col_data];
}
return '';
}
/**
* Write session data
* @param string $session_id The session id.
* @param string $session_data <p>
* @return bool
*/
public function write($session_id, $session_data) {
$sql = "INSERT INTO $this->table_sess SET
$this->col_sid=" . $this->db->quote($session_id) .",
$this->col_expiry=" . $this->db->quote($this->expiry) . ",
$this->col_data=" . $this->db->quote($session_data) . "
ON DUPLICATE KEY UPDATE
$this->col_data=" . $this->db->quote($session_data);
$this->db->query($sql);
return true;
}
/**
* Close the session
* @return bool
*/
public function close() {
return true;
}
/**
* Garbage collection
* @param int $maxlifetime
* @return bool
*/
public function gc($maxlifetime) {
$sql = "DELETE FROM $this->table_sess
WHERE $this->col_expiry <" . time();
$this->db->query($sql);
return true;
}
/**
* Destroy a session
* @param string $session_id The session ID being destroyed.
* @return bool
*/
public function destroy($session_id) {
$sql = "DELETE FROM $this->table_sess
WHERE $this->col_sid=" . $this->db->quote($session_id);
$this->db->query($sql);
return true;
}
}