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
|
<?php
// class DatabaseSession implements Session_interface
class DatabaseSession implements SessionHandlerInterface
{
/** PROPERTIES
* -------------------------------------------------------------------------
*/
private $db; // the database object
// private $db_driver; // the database driver
/** METHODS
* -------------------------------------------------------------------------
*/
/** __construct
* set database connection
* set session handler to overide default session
* start session
*/
public function __construct()
{
// Prepare the Database object
$this->db = Registry::use('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_name(SESSION_NAME);
session_start();
}
// public function open(string $path, string $name) : bool
// NOTE: $path and $name are unsued
public function open() : bool
{
if ($this->db) {
return true;
}
else {
// database connection may be closed from another class
// (ex. from some user class); in this case...
// create a new database connection, then recheck.
$conn = new Database();
// $this->db = $conn->connect();
if ($conn) {
$this->db = $conn;
return true;
}
}
return false;
}
public function close() : bool
{
// DEPRECATED: the database object is shared (don;t clode it!)
// // Close the database connection
// if ($this->db = null) { return true; }
// else return false;
return true;
}
public function read(string $id) : string|false
{
$exist = $this->db->runQuery(
"SELECT data FROM sessions WHERE id = :id",
[':id', $id]
);
if (($exist === false) || (count($exist) == 0)) {
return false;
} else {
$data = $exist[0];
}
if (is_null($row['data'])) {
return '';
}
return $row['data'];
}
public function write(string $id, string $data) : bool
{
// Create timestamp
$access = time();
$check = $this->db->runQuery(
"REPLACE INTO sessions VALUES (:id, :access, :data)",
[':id' => $id, ':access' => $access, ':data' => $data ]
);
return ($check == false) ? false : true;
}
public function destroy(string $sassionID) : bool
{
$check = $this->db->runQuery(
'DELETE FROM sessions WHERE id = :id',
[':id' => $sassionID]
);
return ($check == false) ? false : true;
}
public function gc(int $max)
{
// Calculate what is to be deemed old
$old = time() - $max;
$check = $this->db->runQuery(
'DELETE FROM sessions WHERE access < :old',
[':old' => $old]
);
return ($check == false) ? false : 1;
// check garbage-collector probability to run
// echo "probability: ". ini_get("session.gc_probability") ." / ". ini_get("session.gc_divisor") . ", ttl: ". ini_get("session.gc_maxlifetime"); die();
}
# callable $create_sid = ?,
# callable $validate_sid = ?,
# callable $update_timestamp = ?
}
|