Session.class.php
6.57 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?php
class Util_Common_Session implements Util_Session_SaveHandler_Interface
{
const LIFETIME = 1800;
const DOMAIN = '.yoho.cn';
protected $prefix = 'yh_session_';
protected $domain = '.yoho.cn';
protected $_lifetime = false;
protected $_overrideLifetime = false;
protected $_sessionSavePath = '';
protected $_sessionName = '';
protected $mem = null;
/**
* 初始化
*
* @param array $options
*/
public function __construct(array $options = array())
{
$this->domain = defined('SITE_DOMAIN') ? SITE_DOMAIN : self::DOMAIN;
ini_set('session.cookie_domain', $this->domain);
// 使用 COOKIE 保存 SESSION ID 的方式
ini_set('session.use_cookies', 1);
// 兼容使用 GET/POST 变量方式
ini_set('session.use_trans_sid', 1);
// 设置垃圾收集的触发几率
ini_set('session.gc_probability', 1);
// 设置垃圾收集的触发几率除数
ini_set('session.gc_divisor', 100);
// 设置垃圾回收最大生存时间,浏览器生存周期
ini_set('session.gc_maxlifetime', self::LIFETIME);
ini_set('session.cookie_path', '/');
// 多主机共享保存 SESSION ID 的 COOKIE
// 将 session.save_handler 设置为 user
// session_module_name ( 'user' );
// 定义 SESSION 各项操作所对应的方法名;
$this->createCacheObj();
$this->setLifetime(self::LIFETIME);
$this->setOverrideLifetime(true);
}
/**
* 析构函数
*
* @return void
*/
public function __destruct()
{
session_write_close();
$this->mem = null;
}
/**
* 创建Cache对象
*
* @return Instance of cache | false
*/
private function createCacheObj()
{
$this->mem = Util_Cache::factory ( array ('domain' => 'www' . $this->domain, 'class' => 'session') );
if (! isset ( $this->mem ) || ! $this->mem)
{
throw new Framework_YException ( 'All cache servers down.' );
}
return $this->mem;
}
/**
* Set session lifetime and optional whether or not the lifetime of an existing session should be overridden
*
* $lifetime === false resets lifetime to session.gc_maxlifetime
*
* @param int $lifetime
* @param boolean $overrideLifetime (optional)
* @return Zend_Session_SaveHandler_Memcached
*/
public function setLifetime($lifetime, $overrideLifetime = null)
{
if ($lifetime < 0)
{
throw new Framework_YException ( '生存周期不能设置为小于0!' );
}
else if (empty ( $lifetime ))
{
$this->_lifetime = ( int ) ini_get ( 'session.gc_maxlifetime' );
}
else
{
$this->_lifetime = ( int ) $lifetime;
}
if ($overrideLifetime != null)
{
$this->setOverrideLifetime ( $overrideLifetime );
}
return $this;
}
/**
* Retrieve session lifetime
*
* @return int
*/
public function getLifetime()
{
return $this->_lifetime;
}
/**
* Set whether or not the lifetime of an existing session should be overridden
*
* @param boolean $overrideLifetime
* @return Zend_Session_SaveHandler_Memcached
*/
public function setOverrideLifetime($overrideLifetime)
{
$this->_overrideLifetime = ( boolean ) $overrideLifetime;
return $this;
}
public function getOverrideLifetime()
{
return $this->_overrideLifetime;
}
/**
* Retrieve session lifetime considering
*
* @param array $value
* @return int
*/
public function open($save_path, $name)
{
$this->_sessionSavePath = $save_path;
$this->_sessionName = $name;
return true;
}
/**
* Retrieve session expiration time
*
* @param * @param array $value
* @return int
*/
public function close()
{
return true;
}
/**
* 读取接口
*
* @param string $id
* @return mixed | false
*/
public function read($id)
{
$return = '';
$value = $this->mem->get ( $this->prefix . $id ); //获取数据
if ($value)
{
if ($this->_getExpirationTime ( $value ) > time ())
{
$return = $value ['data'];
}
else
{
$this->destroy ( $id );
}
}
return $return;
}
/**
* 写入接口
*
* @param string $id
* @param mixed $data
* @return boolean
*/
public function write($id, $data)
{
$return = false;
$insertDate = array ('modified' => time (), 'data' => ( string ) $data );
$value = $this->mem->get ( $this->prefix . $id ); //获取数据
if ($value)
{
$insertDate ['lifetime'] = $this->_getLifetime ( $value );
$return = $this->_overrideLifetime ? $this->mem->set ( $this->prefix . $id, $insertDate, 0, $insertDate ['lifetime'] ) : $this->mem->set ( $this->prefix . $id, $insertDate );
}
else
{
$insertDate ['lifetime'] = $this->_lifetime;
$return = $this->mem->set ( $this->prefix . $id, $insertDate, 0, $this->_lifetime );
}
return $return;
}
/**
* 摧毁ITEM
*
* @param string $id
* @return boolean
*/
public function destroy($id)
{
return $this->mem->delete ( $this->prefix . $id );
}
/**
* 垃圾收集
*
* @param integer $maxlifetime
* @return true
*/
public function gc($maxlifetime)
{
return true;
}
/**
* 获取生存时间
*
* @param String $value
* @return Integer
*/
protected function _getLifetime($value)
{
$return = $this->_lifetime;
if (! $this->_overrideLifetime)
{
$return = ( int ) $value ['lifetime'];
}
return $return;
}
protected function _getExpirationTime($value)
{
return ( int ) $value ['modified'] + $this->_getLifetime ( $value );
}
}