Session.class.php
7 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
<?php
/**
* Session service
* @name Service_Memory_Session
* @version 1.0 (2013-01-11)
* @package Service/Memory/Session
* @author zhouqq
* @since 1.0
*/
class Service_Session extends Lib_Service
{
const ROUTER = 'auth.yh_uuc.session';
const EXIPRE = 3600;
const PERSIST_LIFE = 14400; //记住自己的session的最大生存周期,暂定4个小时,考虑数据安全。
const BROWSER_LIFE = 3600; //浏览器生存周期的session得最大数据库生存时间,暂定1小时。
//tbl_session表缓存
private static $_tag_tbl_session = 'tag_yh_uuc_tbl_session_';
private static $_key_tbl_session = 'key_yh_uuc_tbl_session_';
/**
* 创建一个系统session
*
* @param $uid integer
* @param $persist boolean
* @return array false
*/
public function createSession ($uid, $persist = true)
{
$time = time();
$params = array();
$params['uid'] = $uid;
$params['sessionkey'] = Lib_Utils::getSessionKey($uid);
$params['lasttime'] = $time;
$params['createtime'] = $time;
$params['createip'] = Util_Utils_Ip::GetRealRemoteIp();
$params['persist'] = $persist ? 'y' : 'n';
$params['postid'] = Lib_Utils::getUniqId();
$params['logoutid'] = Lib_Utils::getUniqId();
$params['src'] = $this->getAgentTxt(true);
$status = self::service(self::ROUTER)->cache(false)->insert('createSession', $params)->status();
if($status)
{
self::service(self::ROUTER)->delTags(self::$_tag_tbl_session);
return $params;
}
else
{
return false;
}
}
/**
* 删除系统session
*
* @param $session_key string
* @return boolean
*/
public function deleteSession ($sessionkey)
{
$status = self::service(self::ROUTER)->delete('deleteSession', array('sessionkey' => $sessionkey))->status();
if($status)
{
self::service(self::ROUTER)->delTags(self::$_tag_tbl_session);
}
return $status;
}
/**
* 根据用户来删除session
*
* @param $session_key string
* @return boolean
*/
public function deleteSessionByUid ($uid)
{
$status = self::service(self::ROUTER)->delete('deleteSessionByUid', array('uid' => $uid))->status();
if($status)
{
self::service(self::ROUTER)->delTags(self::$_tag_tbl_session);
}
return $status;
}
/**
* 得到session全部信息
*
* @param $session_key string
* @return array false
*/
public function getSession ($sessionkey)
{
$key = self::$_key_tbl_session.$sessionkey;
return self::service(self::ROUTER)->tag(self::$_tag_tbl_session)->key($key)->expire(self::EXIPRE)->fetchRow('getSession', array('sessionkey' => $sessionkey));
}
/**
* 更新sessoin信息
*
* @param $sessionkey string
* @param $params array
* @return boolean
*/
public function updateSession ($sessionkey, $params)
{
$replaces = array('upstring' => Util_Utils_SqlString::mergeSqlUpstring($params));
$params['sessionkey'] = $sessionkey;
$status = self::service(self::ROUTER)->update('updateSession', $params, $replaces)->status();
if($status)
{
self::service(self::ROUTER)->delTags(self::$_tag_tbl_session);
}
return $status;
}
/**
* 得到应该垃圾收集的session
*
* @return array false
*/
public function getGcSessions ()
{
$time = time();
$params = array();
$params['templife'] = $time - self::PERSIST_LIFE;
$params['persistlife'] = $time - self::BROWSER_LIFE;
return self::service(self::ROUTER)->cache(false)->fetchCol('getGcSessions', $params);
}
/**
* 得到需要垃圾收集的session数量
*
* @return integer
*/
public function getGcSessionsCounter ()
{
$time = time();
$params = array();
$params['templife'] = $time - self::PERSIST_LIFE;
$params['persistlife'] = $time - self::BROWSER_LIFE;
return (int) self::service(self::ROUTER)->cache(false)->fetchOne('getGcSessionsCounter', $params);
}
/**
* 获取多个sessions
*
* @param string $uids
* @return array
*/
public function getUserSessions ($uids)
{
$rs = substr(str_repeat('?,', count($uids)), 0, - 1);
$key = self::$_key_tbl_session.$uids;
return self::service(self::ROUTER)->tag(self::$_tag_tbl_session)->key($key)->expire(self::EXIPRE)->fetchCol('getUserSessions', $uids, array('rs' => $rs));
}
/**
* 获取用户最新一条session
*
* @param string $uid
* @return string
*/
public function getUserLastSession($uid)
{
$key = self::$_key_tbl_session.'lastSession_'.$uid;
return self::service(self::ROUTER)->tag(self::$_tag_tbl_session)->key($key)->expire(self::EXIPRE)->fetchOne('getUserLastSession', array('uid' => $uid));
}
/**
* 垃圾收集
*
* 暂时没有使用
*
* @return boolean
*/
public function gcSessions ()
{
$time = time();
$params = array();
$params['templife'] = $time - self::PERSIST_LIFE;
$params['persistlife'] = $time - self::BROWSER_LIFE;
$keys = $this->getGcSessions();
if ($keys)
{
$keys = $this->_getRealKeys($keys);
self::service(self::ROUTER)->delTags(self::$_tag_tbl_session);
return self::service(self::ROUTER)->delete('gcSessions', $params)->status();
}
else
{
return true;
}
}
/**
* 得到实际的键
*
* @param $keys array
* @return array
*/
private function _getRealKeys ($keys)
{
$return = array();
foreach ($keys as $v)
{
$return[] = self::$key_session . $v;
}
return $return;
}
/**
* 获取访问终端信息
*
* @return string
*/
public function getAgentTxt ($int = false)
{
$agent = isset($_SERVER['HTTP_USER_AGENT']) ?$_SERVER['HTTP_USER_AGENT'] : '' ;
$agent = Util_StringHelper::toUtf8($agent);
$ret = 'web';
if (is_string($agent))
{
if (preg_match('/(Android .+)\;/sU', $agent))
{
$ret = 'android';
}
else if (stripos($agent, 'iPad') !== false)
{
$ret = 'ios';
}
else if (stripos($agent, 'iPod touch') !== false)
{
$ret = 'ios';
}
else if (stripos($agent, 'iPhone') !== false)
{
$ret = 'ios';
}
else
{
$ret = 'web';
}
}
if ($int)
{
$list = array('web' => 1, 'android' => 2, 'ios' => 3);
return isset($list[$ret])?$list[$ret]:1 ;
}
return $ret ;
}
}
?>