-
Notifications
You must be signed in to change notification settings - Fork 42
/
mysql.sessions.php
140 lines (130 loc) · 2.88 KB
/
mysql.sessions.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
<?php
/*
Revised code by Dominick Lee
Original code derived from "Essential PHP Security" by Chriss Shiflett
Last Modified 2/27/2017
CREATE TABLE sessions
(
id varchar(32) NOT NULL,
access int(10) unsigned,
data text,
PRIMARY KEY (id)
);
+--------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+------------------+------+-----+---------+-------+
| id | varchar(32) | | PRI | | |
| access | int(10) unsigned | YES | | NULL | |
| data | text | YES | | NULL | |
+--------+------------------+------+-----+---------+-------+
*/
class Session {
private $db;
public function __construct(){
// Instantiate new Database object
$this->db = new Database;
// Set handler to overide SESSION
session_set_save_handler(
array($this, "_open"),
array($this, "_close"),
array($this, "_read"),
array($this, "_write"),
array($this, "_destroy"),
array($this, "_gc")
);
// Start the session
session_start();
}
public function _open(){
// If successful
if($this->db)
{
// Return True
return true;
}
// Return False
return false;
}
public function _close(){
// Close the database connection
// If successful
if($this->db->close())
{
// Return True
return true;
}
// Return False
return false;
}
public function _read($id){
// Set query
$this->db->query('SELECT data FROM sessions WHERE id = :id');
// Bind the Id
$this->db->bind(':id', $id);
// Attempt execution
// If successful
if($this->db->execute())
{
if($this->db->rowCount() > 0)
{
// Save returned row
$row = $this->db->single();
// Return the data
return $row['data'];
}
}
// Return an empty string
return '';
}
public function _write($id, $data){
// Create time stamp
$access = time();
// Set query
$this->db->query('REPLACE INTO sessions VALUES (:id, :access, :data)');
// Bind data
$this->db->bind(':id', $id);
$this->db->bind(':access', $access);
$this->db->bind(':data', $data);
// Attempt Execution
// If successful
if($this->db->execute())
{
// Return True
return true;
}
// Return False
return false;
}
public function _destroy($id){
// Set query
$this->db->query('DELETE FROM sessions WHERE id = :id');
// Bind data
$this->db->bind(':id', $id);
// Attempt execution
// If successful
if($this->db->execute())
{
// Return True
return true;
}
// Return False
return false;
}
public function _gc($max){
// Calculate what is to be deemed old
$old = time() - $max;
// Set query
$this->db->query('DELETE FROM sessions WHERE access < :old');
// Bind data
$this->db->bind(':old', $old);
// Attempt execution
if($this->db->execute())
{
// Return True
return true;
}
// Return False
return false;
}
}
?>