SessionStoreSqlite.php
3.3 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
141
142
<?php
class LtSessionSqlite implements LtSessionStore
{
public $conf;
private $lifeTime; //session.gc_maxlifetime
private $dbHandle;
private $dbName;
private $tableName;
public function __construct()
{
// --
}
public function init()
{
if (isset($this->conf['gc_maxlifetime']))
{
$this->lifeTime = $this->conf['gc_maxlifetime'];
}
else
{
$this->lifeTime = get_cfg_var("session.gc_maxlifetime");
}
if (isset($this->conf['table_name']))
{
$this->tableName = $this->conf['table_name'];
}
else
{
$this->tableName = 'lotus_session';
}
if (isset($this->conf['db_name']))
{
$this->dbName = $this->conf['db_name'];
}
else
{
$this->dbName = '/tmp/Lotus/session/session_sqlite2.db';
}
if (!$this->dbHandle = sqlite_open($this->dbName, 0666))
{
trigger_error('session sqlite db error');
return false;
}
return true;
}
public function open($savePath, $sessName)
{
return true;
}
public function close()
{
$this->gc($this->lifeTime);
return @sqlite_close($this->dbHandle);
}
public function read($sessID)
{
$res = sqlite_query("SELECT session_data AS d FROM $this->tableName
WHERE session_id = '$sessID'
AND session_expires > " . time(), $this->dbHandle);
if ($row = sqlite_fetch_array($res, SQLITE_ASSOC))
{
return $row['d'];
}
else
{
return "";
}
}
public function write($sessID, $sessData)
{
$newExp = time() + $this->lifeTime;
$res = sqlite_query("SELECT * FROM $this->tableName
WHERE session_id = '$sessID'", $this->dbHandle);
if (sqlite_num_rows($res))
{
sqlite_exec("UPDATE $this->tableName
SET session_expires = '$newExp',
session_data = '$sessData'
WHERE session_id = '$sessID'", $this->dbHandle);
if (sqlite_changes($this->dbHandle))
{
return true;
}
}
else
{
sqlite_exec("INSERT INTO $this->tableName (
session_id,
session_expires,
session_data)
VALUES(
'$sessID',
'$newExp',
'$sessData')", $this->dbHandle);
if (sqlite_changes($this->dbHandle))
{
return true;
}
}
return false;
}
public function destroy($sessID)
{
sqlite_exec("DELETE FROM $this->tableName WHERE session_id = '$sessID'", $this->dbHandle);
if (sqlite_changes($this->dbHandle))
{
return true;
}
return false;
}
public function gc($sessMaxLifeTime)
{
sqlite_exec("DELETE FROM $this->tableName WHERE session_expires < " . time(), $this->dbHandle);
return sqlite_changes($this->dbHandle);
}
public function runOnce()
{
$sql = "SELECT name FROM sqlite_master WHERE type='table' UNION ALL SELECT name FROM sqlite_temp_master WHERE type='table' AND name='" . $this->tableName . "'";
$res = sqlite_query($sql, $this->dbHandle);
$row = sqlite_fetch_array($res, SQLITE_ASSOC);
if (empty($row))
{
$sql = "CREATE TABLE $this->tableName (
[session_id] VARCHAR(255) NOT NULL PRIMARY KEY,
[session_expires] INTEGER DEFAULT '0' NOT NULL,
[session_data] TEXT NULL
)";
return sqlite_exec($sql, $this->dbHandle);
}
return false;
}
}