-
Notifications
You must be signed in to change notification settings - Fork 0
/
session_file.php
174 lines (144 loc) · 3.25 KB
/
session_file.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php if ( ! defined('AB_SDK_WORK_DIR')) exit('No direct script access allowed');
//文件内存操作类,适用于AP端
class SessionFile {
public $_f; //文件句柄
private $_file_path="/tmp/sess"; // ac /tmp/portal_sess/
public $session_ttl = 900; //seconds
public function __construct($file_path = '/tmp/sess'){
$this->_file_path = $file_path;
if(!is_dir($this->_file_path)){
mkdir($this->_file_path,777,TRUE);
}
//删除旧文件
$this->delete_files($this->_file_path);
}
public function setex($key, $ttl, $data){
$_file = $this->_file_path. DIRECTORY_SEPARATOR . $key;
$contents = array(
'time' => time(),
'ttl' => $ttl,
'data' => $data
);
if ($this->write_file($_file, serialize($contents)))
{
return TRUE;
}
return FALSE;
}
public function expire($key, $ttl){
$_file = $this->_file_path. DIRECTORY_SEPARATOR . $key;
if ( ! file_exists($_file))
{
return FALSE;
}
$data = $this->read_file($_file);
$data = unserialize($data);
$data['time'] = time();
$data['ttl'] = $ttl;
if ($this->write_file($_file, serialize($data)))
{
return TRUE;
}
return FALSE;
}
public function get($key){
$_file = $this->_file_path. DIRECTORY_SEPARATOR . $key;
if ( ! file_exists($_file))
{
return FALSE;
}
$data = $this->read_file($_file);
$data = unserialize($data);
if (time() > $data['time'] + $data['ttl'])
{
unlink($_file);
return FALSE;
}
return $data['data'];
}
public function ttl($key) {
$_file = $this->_file_path. DIRECTORY_SEPARATOR . $key;
if ( ! file_exists($_file))
{
return FALSE;
}
$data = $this->read_file($_file);
$data = unserialize($data);
$ttl = time() - $data['time'];
if ($ttl < 0 || $ttl >= $data['ttl']) {
return FALSE;
} else {
return $data['ttl'] - $ttl;
}
}
private function write_file($path, $data, $mode = 'wb')
{
if ( ! $fp = @fopen($path, $mode))
{
return FALSE;
}
flock($fp, LOCK_EX);
fwrite($fp, $data);
flock($fp, LOCK_UN);
fclose($fp);
return TRUE;
}
private function read_file($file)
{
if ( ! file_exists($file))
{
return FALSE;
}
if (function_exists('file_get_contents'))
{
return file_get_contents($file);
}
if ( ! $fp = @fopen($file, FOPEN_READ))
{
return FALSE;
}
flock($fp, LOCK_SH);
$data = '';
if (filesize($file) > 0)
{
$data =& fread($fp, filesize($file));
}
flock($fp, LOCK_UN);
fclose($fp);
return $data;
}
private function delete_files($path, $del_dir = FALSE, $level = 0)
{
// Trim the trailing slash
$path = rtrim($path, DIRECTORY_SEPARATOR);
if ( ! $current_dir = @opendir($path))
{
return FALSE;
}
while (FALSE !== ($filename = @readdir($current_dir)))
{
if ($filename != "." and $filename != "..")
{
if (is_dir($path.DIRECTORY_SEPARATOR.$filename))
{
// Ignore empty folders
if (substr($filename, 0, 1) != '.')
{
$this->delete_files($path.DIRECTORY_SEPARATOR.$filename, $del_dir, $level + 1);
}
}
else
{
$_file = $path.DIRECTORY_SEPARATOR.$filename;
$_mtime = filemtime($_file);
$_ts = time();
if(($_mtime + $this->session_ttl) < $_ts){
unlink($_file);
}
}
}
}
@closedir($current_dir);
return TRUE;
}
}