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
/** FileSessionHandler
* is a custom File-based Session Handler
*
* Could be useful when implemented along with data-cryptography,
* otherwise php's default session handler (which is also file-based)
* seems to be the obvious way to go;
*
* NOTE:
* if shared sessions accros an array of servers is needed,
* a database-session handler is probably the best choice.
*
* TODO:
* implement cryptography
*/
class FileSessionHandler implements SessionHandlerInterface
{
/** The filesystem instance. */
protected $sessionName;
/** The path where all sessions should be stored. */
protected $path;
/** The number of minutes the session should be valid. */
protected $minutes;
/**
* Create a new file driven session-handler instance.
*
* @param string $path
* @param int $minutes
* @return void
*/
public function __construct()
{
// values comming from configuration
$this->sess_filename = $path;
$this->minutes = $minutes;
// Set handler to overide SESSION
session_set_save_handler(
array($this, "open"),
array($this, "close"),
array($this, "read"),
array($this, "write"),
array($this, "destroy"),
array($this, "gc")
);
// Start the session
session_name(SESSION_NAME);
session_start();
}
/** open
* set filename;
* no need to touch the filesystem yet
* return true (always)
*/
public function open($savePath, $sessionName): bool
{
$this->sess_filename = $this->$path .'/'. $sessionName;
return true;
}
/** close
* nothing needs to be closed;
* return true (always)
*/
public function close(): bool
{
return true;
}
/** read
* chech if file exists; if not return false
* read data
*/
public function read($sessionId): string|false
{
if (!file_exists($filename) || !is_readable($filename)) return false;
return file_get_contents($filename);
# if ($this->files->isFile($path = $this->path.'/'.$sessionId) &&
# $this->files->lastModified($path) >= Carbon::now()->subMinutes($this->minutes)->getTimestamp()) {
# return $this->files->sharedGet($path);
# }
# depricated? return '';
}
/** write
* data serielized already by php's internal session engine
*/
public function write($sessionId, $data): bool
{
$h = fopen($filename, 'w');
if (fwrite($h,$data) === false) {
throw new Exception('Could not write session data');
return false;
}
fclose($h);
return true;
}
/**
* {@inheritdoc}
*
* @return bool
*/
public function destroy($sassionID): bool
{
unlink($this->sassionID);
return true;
}
/**
* {@inheritdoc}
*
* @return int
*/
public function gc($lifetime) : int
{
$files = Finder::create()
->in($this->path)
->files()
->ignoreDotFiles(true)
->date('<= now - '.$lifetime.' seconds');
$deletedSessions = 0;
foreach ($files as $file) {
$this->files->delete($file->getRealPath());
$deletedSessions++;
}
return $deletedSessions;
}
}
|