forked from phpcoinn/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommonSessionHandler.php
More file actions
76 lines (67 loc) · 1.89 KB
/
CommonSessionHandler.php
File metadata and controls
76 lines (67 loc) · 1.89 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
<?php
class CommonSessionHandler implements SessionHandlerInterface {
private $path;
#[\ReturnTypeWillChange]
public function close()
{
return true;
}
#[\ReturnTypeWillChange]
public function destroy($id)
{
// _log("Dapps: destroy session");
$sess_file = $this->path."/sess_$id";
if (!file_exists($sess_file)) return false;
$ret = @unlink($sess_file);
return $ret;
}
#[\ReturnTypeWillChange]
public function gc($max_lifetime)
{
$deleted=0;
_log("Dapps: call session gc");
foreach (glob($this->path."/sess_*") as $filename) {
if (filemtime($filename) + $max_lifetime < time()) {
$res= @unlink($filename);
if($res) {
$deleted++;
}
}
}
return $deleted;
}
#[\ReturnTypeWillChange]
public function open($path, $name)
{
$this->path = $path;
return(true);
}
#[\ReturnTypeWillChange]
public function read($id)
{
$out = "";
$sess_file = $this->path."/sess_$id";
if(file_exists($sess_file)) $out=@file_get_contents($sess_file);
return (string) $out;
}
#[\ReturnTypeWillChange]
public function write($id, $data)
{
if(empty($data)) return true;
$ret= file_put_contents($this->path."/sess_$id", $data) === false ? false : true;
return $ret;
}
static function setup($session_id = null) {
$handler = new CommonSessionHandler();
session_set_save_handler($handler, true);
$sessions_dir = ROOT."/tmp/sessions";
if (!is_dir($sessions_dir)) {
@mkdir($sessions_dir, 0755, true);
}
session_save_path($sessions_dir);
if(!empty($session_id)) {
session_id($session_id);
}
@session_start();
}
}