]>
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) { | |
e7f73cf6 MR |
23 | $result = file_put_contents("$this->savePath/qt_sess_$id", $data); |
24 | chmod("$this->savePath/qt_sess_$id", 0600); | |
25 | ||
26 | return ($result === false) ? false : true; | |
630144db MR |
27 | } |
28 | ||
29 | function destroy($id) { | |
30 | $file = "$this->savePath/qt_sess_$id"; | |
31 | if (file_exists($file)) { | |
32 | unlink($file); | |
33 | } | |
34 | ||
35 | return true; | |
36 | } | |
37 | ||
38 | function gc($maxlifetime) { | |
39 | foreach (glob("$this->savePath/qt_sess_*") as $file) { | |
40 | if (filemtime($file) + $maxlifetime < time() && file_exists($file)) { | |
41 | unlink($file); | |
42 | } | |
43 | } | |
44 | ||
45 | return true; | |
46 | } | |
47 | } | |
48 | ||
49 | $handler = new FileSessionHandler(); | |
50 | session_set_save_handler( | |
51 | array($handler, 'open'), | |
52 | array($handler, 'close'), | |
53 | array($handler, 'read'), | |
54 | array($handler, 'write'), | |
55 | array($handler, 'destroy'), | |
56 | array($handler, 'gc') | |
57 | ); | |
58 | ||
59 | // the following prevents unexpected effects when using objects as save handlers | |
60 | register_shutdown_function('session_write_close'); | |
61 | ?> |