-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySessionHandler.php
More file actions
101 lines (90 loc) · 2.27 KB
/
Copy pathMySessionHandler.php
File metadata and controls
101 lines (90 loc) · 2.27 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
<?php
namespace Detain\SessionSamurai;
class MySessionHandler implements \SessionHandlerInterface, \SessionIdInterface, \SessionUpdateTimestampHandlerInterface
{
private string $savePath = '';
/**
* {@inheritdoc}
*/
public function open($savePath, $sessionName): bool
{
$this->savePath = $savePath;
return true;
}
/**
* {@inheritdoc}
*/
public function close(): bool
{
return true;
}
/**
* {@inheritdoc}
*/
public function read(string $sessionId): string
{
$sessionFile = $this->savePath . '/sess_' . $sessionId;
if (file_exists($sessionFile)) {
$data = file_get_contents($sessionFile);
return $data !== false ? $data : '';
}
return '';
}
/**
* {@inheritdoc}
*/
public function write($sessionId, $data): bool
{
$sessionFile = $this->savePath . '/sess_' . $sessionId;
return file_put_contents($sessionFile, $data) !== false;
}
/**
* {@inheritdoc}
*/
public function destroy($sessionId): bool
{
$sessionFile = $this->savePath . '/sess_' . $sessionId;
if (file_exists($sessionFile)) {
unlink($sessionFile);
}
return true;
}
/**
* {@inheritdoc}
*/
public function gc(int $maxlifetime): int|false
{
$count = 0;
foreach (glob($this->savePath . '/sess_*') ?: [] as $file) {
if (filemtime($file) + $maxlifetime < time() && file_exists($file)) {
unlink($file);
$count++;
}
}
return $count;
}
/**
* {@inheritdoc}
*/
// phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
public function create_sid(): string
{
return bin2hex(random_bytes(32));
}
/**
* {@inheritdoc}
*/
public function validateId(string $sessionId): bool
{
$sessionFile = $this->savePath . '/sess_' . $sessionId;
return file_exists($sessionFile);
}
/**
* {@inheritdoc}
*/
public function updateTimestamp(string $sessionId, string $sessionData): bool
{
$sessionFile = $this->savePath . '/sess_' . $sessionId;
return touch($sessionFile);
}
}