-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFileSessionHandler.php
64 lines (47 loc) · 1.73 KB
/
FileSessionHandler.php
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
<?php
namespace Rakit\Session;
use SessionHandlerInterface;
use Exception;
class FileSessionHandler implements SessionHandlerInterface {
protected $sess_path;
protected $prefix;
protected $postfix;
public function __construct($sess_path, $prefix = 'sess_', $postfix = '') {
if (!is_dir($sess_path)) {
throw new Exception("Cannot use FileSessionHandler, directory '{$sess_path}' not found", 1);
}
if (!is_writable($sess_path)) {
throw new Exception("Cannot use FileSessionHandler, directory '{$sess_path}' is not writable", 2);
}
$this->sess_path = $sess_path;
$this->prefix = $prefix;
$this->postfix = $postfix;
}
public function open($save_path, $sess_name) {
return true;
}
public function close() {
return true;
}
public function read($sess_id) {
return (string)@file_get_contents("{$this->sess_path}/{$this->prefix}{$sess_id}{$this->postfix}");
}
public function write($sess_id, $data) {
return file_put_contents("{$this->sess_path}/{$this->prefix}{$sess_id}{$this->postfix}", $data) === false ? false : true;
}
public function destroy($sess_id) {
$file = "{$this->sess_path}/{$this->prefix}{$sess_id}{$this->postfix}";
if (file_exists($file)) {
unlink($file);
}
return true;
}
public function gc($lifetime) {
foreach (glob("{$this->sess_path}/{$this->prefix}*") as $file) {
if (filemtime($file) + $lifetime < time() && file_exists($file)) {
unlink($file);
}
}
return true;
}
}