-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSessionManager.php
More file actions
89 lines (81 loc) · 2.35 KB
/
FileSessionManager.php
File metadata and controls
89 lines (81 loc) · 2.35 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
<?php
require_once 'BaseSessionManager.php';
/**
* Class for accessing to file session storage (PHP default)
*/
class FileSessionManager extends BaseSessionManager
{
/**
* Path of the PHP session directory (ini: session_save_path)
* @var string
*/
private $path;
/**
* Connect to session storage
* @param array $params array('path' => '')
*/
public function connect(array $params)
{
if (empty($params['path'])) {
throw new InvalidArgumentException('path not found');
}
$this->path = $params['path'];
if (!file_exists($this->path) || !is_dir($this->path)) {
throw new LogicException('session path directory is not found') ;
}
if (!is_readable($this->path)) {
throw new LogicException('session path is not readable');
}
}
public function get($key)
{
$key = strval($key);
if (empty($key)) {
throw new InvalidArgumentException('key is empty');
}
$session_filename = $this->path . $this->getPrefix() . $key;
return file_get_contents($session_filename);
}
public function set($key, $value)
{
$key = strval($key);
if (empty($key)) {
throw new InvalidArgumentException('key is empty');
}
$session_filename = $this->path . $this->getPrefix() . $key;
return file_put_contents($session_filename, $value);
}
public function delete($key)
{
$key = strval($key);
if (empty($key)) {
throw new InvalidArgumentException('key is empty');
}
$session_filename = $this->path . $this->getPrefix() . $key;
return unlink($session_filename);
}
public function deleteAll()
{
$keys = $this->getAllKeys();
$count = 0;
foreach ($keys as $key) {
$this->delete($key);
$count++;
}
return $count;
}
public function getAllKeys()
{
$sessions = scandir($this->path);
if (!$sessions) {
return array();
}
$ret = array();
foreach($sessions as $s) {
if(!in_array($s, array('.', '..')) && starts_with($s, $this->getPrefix())) {
$ret[] = str_replace($this->getPrefix(), '', $s);
}
}
return $ret;
}
}