-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSessionHandler.php
More file actions
101 lines (90 loc) · 2.33 KB
/
Copy pathFileSessionHandler.php
File metadata and controls
101 lines (90 loc) · 2.33 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 FileSessionHandler implements \SessionHandlerInterface, \SessionIdInterface, \SessionUpdateTimestampHandlerInterface
{
private string $savePath = '';
/**
* {@inheritdoc}
*/
public function close(): bool
{
// return value should be true for success or false for failure
return true;
}
/**
* {@inheritdoc}
*/
public function destroy(string $id): bool
{
$file = "$this->savePath/sess_$id";
if (file_exists($file)) {
unlink($file);
}
// return value should be true for success or false for failure
return true;
}
/**
* {@inheritdoc}
*/
public function gc(int $max_lifetime): int|false
{
$count = 0;
foreach (glob("$this->savePath/sess_*") ?: [] as $file) {
if (filemtime($file) + $max_lifetime < time() && file_exists($file)) {
unlink($file);
$count++;
}
}
return $count;
}
/**
* {@inheritdoc}
*/
public function open(string $path, string $name): bool
{
$this->savePath = $path;
if (!is_dir($this->savePath)) {
mkdir($this->savePath, 0777);
}
// return value should be true for success or false for failure
return true;
}
/**
* {@inheritdoc}
*/
public function read(string $id): string
{
$data = @file_get_contents("$this->savePath/sess_$id");
return $data !== false ? $data : '';
}
/**
* {@inheritdoc}
*/
public function write(string $id, string $data): bool
{
// return value should be true for success or false for failure
return file_put_contents("$this->savePath/sess_$id", $data) === false ? false : true;
}
/**
* {@inheritdoc}
*/
// phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
public function create_sid(): string
{
return bin2hex(random_bytes(32));
}
/**
* {@inheritdoc}
*/
public function updateTimestamp(string $id, string $data): bool
{
return touch("$this->savePath/sess_$id");
}
/**
* {@inheritdoc}
*/
public function validateId(string $id): bool
{
return file_exists("$this->savePath/sess_$id");
}
}