]>
Commit | Line | Data |
---|---|---|
630144db MR |
1 | <?php |
2 | class FileSessionHandler { | |
3 | private $savePath; | |
4 | ||
5 | function open($savePath, $sessionName) { | |
6 | $this->savePath = $savePath; | |
7 | if (!is_dir($this->savePath)) { | |
8 | mkdir($this->savePath, 0777); | |
9 | } | |
10 | ||
11 | return true; | |
12 | } | |
13 | ||
14 | function close() { | |
15 | return true; | |
16 | } | |
17 | ||
18 | function read($id) { | |
19 | return (string)@file_get_contents("$this->savePath/qt_sess_$id"); | |
20 | } | |
21 | ||
22 | function write($id, $data) { | |
23 | return file_put_contents("$this->savePath/qt_sess_$id", $data) === false ? false : true; | |
24 | } | |
25 | ||
26 | function destroy($id) { | |
27 | $file = "$this->savePath/qt_sess_$id"; | |
28 | if (file_exists($file)) { | |
29 | unlink($file); | |
30 | } | |
31 | ||
32 | return true; | |
33 | } | |
34 | ||
35 | function gc($maxlifetime) { | |
36 | foreach (glob("$this->savePath/qt_sess_*") as $file) { | |
37 | if (filemtime($file) + $maxlifetime < time() && file_exists($file)) { | |
38 | unlink($file); | |
39 | } | |
40 | } | |
41 | ||
42 | return true; | |
43 | } | |
44 | } | |
45 | ||
46 | $handler = new FileSessionHandler(); | |
47 | session_set_save_handler( | |
48 | array($handler, 'open'), | |
49 | array($handler, 'close'), | |
50 | array($handler, 'read'), | |
51 | array($handler, 'write'), | |
52 | array($handler, 'destroy'), | |
53 | array($handler, 'gc') | |
54 | ); | |
55 | ||
56 | // the following prevents unexpected effects when using objects as save handlers | |
57 | register_shutdown_function('session_write_close'); | |
58 | ?> |