forked from sprain/PHP-MySQL-Session-Handler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SessionHandler.php
163 lines (120 loc) · 3.68 KB
/
SessionHandler.php
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
162
163
<?php
/**
* A PHP session handler to keep session data within a MySQL database
*
* @author Manuel Reinhard <[email protected]>
* @link https://github.com/sprain/PHP-MySQL-Session-Handler
*/
class SessionHandler{
/**
* a database MySQLi connection resource
* @var resource
*/
protected $dbConnection;
/**
* the name of the DB table which handles the sessions
* @var string
*/
protected $dbTable;
/**
* Set db data if no connection is being injected
* @param string $dbHost
* @param string $dbUser
* @param string $dbPassword
* @param string $dbDatabase
*/
public function setDbDetails($dbHost, $dbUser, $dbPassword, $dbDatabase){
//create db connection
$this->dbConnection = new mysqli($dbHost, $dbUser, $dbPassword, $dbDatabase);
//check connection
if (mysqli_connect_error()) {
throw new Exception('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
}//if
}//function
/**
* Inject DB connection from outside
* @param object $dbConnection expects MySQLi object
*/
public function setDbConnection($dbConnection){
$this->dbConnection = $dbConnection;
}
/**
* Inject DB connection from outside
* @param object $dbConnection expects MySQLi object
*/
public function setDbTable($dbTable){
$this->dbTable = $dbTable;
}
/**
* Open the session
* @return bool
*/
public function open() {
//delete old session handlers
$limit = time() - (3600 * 24);
$sql = sprintf("DELETE FROM %s WHERE timestamp < %s", $this->dbTable, $limit);
return $this->dbConnection->query($sql);
}
/**
* Close the session
* @return bool
*/
public function close() {
return $this->dbConnection->close();
}
/**
* Read the session
* @param int session id
* @return string string of the sessoin
*/
public function read($id) {
$sql = sprintf("SELECT data FROM %s WHERE id = '%s'", $this->dbTable, $this->dbConnection->escape_string($id));
if ($result = $this->dbConnection->query($sql)) {
if ($result->num_rows && $result->num_rows > 0) {
$record = $result->fetch_assoc();
return $record['data'];
} else {
return false;
}
} else {
return false;
}
return true;
}
/**
* Write the session
* @param int session id
* @param string data of the session
*/
public function write($id, $data) {
$sql = sprintf("REPLACE INTO %s VALUES('%s', '%s', '%s')",
$this->dbTable,
$this->dbConnection->escape_string($id),
$this->dbConnection->escape_string($data),
time());
return $this->dbConnection->query($sql);
}
/**
* Destoroy the session
* @param int session id
* @return bool
*/
public function destroy($id) {
$sql = sprintf("DELETE FROM %s WHERE `id` = '%s'", $this->dbTable, $this->dbConnection->escape_string($id));
return $this->dbConnection->query($sql);
}
/**
* Garbage Collector
* @param int life time (sec.)
* @return bool
* @see session.gc_divisor 100
* @see session.gc_maxlifetime 1440
* @see session.gc_probability 1
* @usage execution rate 1/100
* (session.gc_probability/session.gc_divisor)
*/
public function gc($max) {
$sql = sprintf("DELETE FROM %s WHERE `timestamp` < '%s'", $this->dbTable, time() - intval($max));
return $this->dbConnection->query($sql);
}
}//class