-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSession_Cli.php
More file actions
51 lines (44 loc) · 1.08 KB
/
Copy pathSession_Cli.php
File metadata and controls
51 lines (44 loc) · 1.08 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
<?php
/**
* 模拟命令行下的session
* 此暂时只保存一条记录
*/
class Session_Cli
{
private $_path;
protected static $_instance = null;
private function __construct() {}
private function __clone() {}
public static function getInstance()
{
if(!is_null(self::$_instance)){
return self::$_instance;
}else{
$instance = new self;
$instance->_path = dirname(__FILE__).'/session';
self::$_instance = $instance;
return $instance;
}
}
public function set($key, $val)
{
$json = json_encode([$key => $val]);
file_put_contents($this->_path, serialize($json));
return true;
}
public function get($key)
{
$session = $this->getContents();
if(isset($session[$key])){
return $session[$key];
}else{
return null;
}
}
public function getContents()
{
$content = file_get_contents($this->_path);
return json_decode(unserialize($content),true);
}
}
?>