-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDualSessionLogger.php
More file actions
140 lines (119 loc) · 4.82 KB
/
DualSessionLogger.php
File metadata and controls
140 lines (119 loc) · 4.82 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
<?php
// implements native `SessionHandlerInterface`(open | close | read | write | destroy | gc)
// Author: Mukul kumar(https://github.com/slimdestro/)
class DualSessionLogger implements SessionHandlerInterface {
private $db;
private $dual_sessions_table;
public function __construct(PDO $db = null, $dual_sessions_table = null) {
if(!$db){
throw new Exception($this->DualSessionLogger_Error("fatal", "DB instance missed in constructor()"));
}else{
$this->db = $db;
$this->dual_sessions_table = $dual_sessions_table;
$this->createTableIfNotExists();
}
}
public function open($savePath, $sessionName) {
return true;
}
public function close() {
return true;
}
// standard implementation read() from SessionHandlerInterface
public function read($sessionId) {
try {
$stmt = $this->db->prepare("SELECT data FROM {$this->dual_sessions_table} WHERE id = :id");
$stmt->bindParam(":id", $sessionId);
$stmt->execute();
if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
return $row['data'];
}
return "";
} catch (PDOException $e) {
throw new Exception($this->DualSessionLogger_Error("warning", $e->getMessage()));
return false;
}
}
public function write($sessionId, $data) {
try {
$timestamp = time();
$stmt = $this->db->prepare("REPLACE INTO {$this->dual_sessions_table}(id, data, timestamp) VALUES (:id, :data, :timestamp)");
$stmt->bindParam(":id", $sessionId);
$stmt->bindParam(":data", $data);
$stmt->bindParam(":timestamp", $timestamp);
$stmt->execute();
return true;
} catch (PDOException $e) {
throw new Exception($this->DualSessionLogger_Error("warning", $e->getMessage()));
return false;
}
}
public function destroy($sessionId) {
try {
$stmt = $this->db->prepare("DELETE FROM {$this->dual_sessions_table} WHERE id = :id");
$stmt->bindParam(":id", $sessionId);
$stmt->execute();
return true;
} catch (PDOException $e) {
throw new Exception($this->DualSessionLogger_Error("warning", $e->getMessage()));
return false;
}
}
public function gc($maxLifetime) {
try {
$oldTimestamp = time() - $maxLifetime;
$stmt = $this->db->prepare("DELETE FROM {$this->dual_sessions_table} WHERE timestamp < :oldTimestamp");
$stmt->bindParam(":oldTimestamp", $oldTimestamp);
$stmt->execute();
return true;
} catch (PDOException $e) {
throw new Exception($this->DualSessionLogger_Error("warning", $e->getMessage()));
return false;
}
}
// load supplied table if doesn't exist
private function createTableIfNotExists() {
try {
if ($this->db) {
if (!$this->dual_sessions_table) {
$sql = "
CREATE TABLE IF NOT EXISTS dual_sessions (
id VARCHAR(128) NOT NULL PRIMARY KEY,
data TEXT,
timestamp INT(11) NOT NULL
);";
$this->db->exec($sql);
$this->dual_sessions_table = "dual_sessions";
}else{
if($this->tableReallyExists($this->dual_sessions_table) == false){
echo $this->DualSessionLogger_Error("fatal", "Table supplied {$this->dual_sessions_table} doesnt exist");
}
}
} else {
echo $this->DualSessionLogger_Error("fatal", "Couldn't create table");
}
} catch (Exception $e) {
throw new Exception($this->DualSessionLogger_Error("fatal", "Failed initializing table..."));
}
}
private function DualSessionLogger_Error($level, $msg) {
if ($level && $msg) {
return "<b>DualSessionLogger_Error(<small style='color:red'>exit({$level})</small>):</b> {$msg}<hr>";
}
}
private function tableReallyExists($tableName) {
try {
$sql = "SHOW TABLES LIKE :table_name";
$stmt = $this->db->prepare($sql);
if ($stmt) {
$stmt->bindParam(':table_name', $tableName, PDO::PARAM_STR);
$stmt->execute();
return $stmt->rowCount() > 0;
} else {
return "SQL Error";
}
} catch (Exception $e) {
return false;
}
}
}