将ecshop中的session机制重写,从DB移植到Memcache中去
来源:互联网 发布:社交网络 迅雷 编辑:程序博客网 时间:2024/05/01 22:28
[代码] [PHP]代码
http://www.oschina.net/code/snippet_83895_6963
001
<?php
002
003
if
(!defined(
'IN_ECS'
))
004
{
005
die
(
'Hacking attempt'
);
006
}
007
008
/*------------------------------------------------------ */
009
//-- 该类用于将SESSION直接写入Memcache
010
/*------------------------------------------------------ */
011
class
cls_session
012
{
013
var
$db
= NULL;
014
015
var
$max_life_time
= 1800;
// SESSION 过期时间
016
017
var
$session_name
=
''
;
018
var
$session_id
=
''
;
019
020
var
$session_expiry
=
''
;
021
var
$session_md5
=
''
;
022
023
var
$session_cookie_path
=
'/'
;
024
var
$session_cookie_domain
=
''
;
025
var
$session_cookie_secure
= false;
026
027
var
$_ip
=
''
;
028
var
$_time
= 0;
029
030
function
__construct(&
$db
,
$session_table
,
$session_data_table
,
$session_name
=
'ECS_ID'
,
$session_id
=
''
)
031
{
032
$m
=
new
Memcache;
033
$m
->addServer(
'127.0.0.1'
, 11211);
034
$this
->cls_session(
$m
,
$session_name
,
$session_id
);
035
}
036
037
function
cls_session(&
$db
,
$session_name
=
'ECS_ID'
,
$session_id
=
''
)
038
{
039
$GLOBALS
[
'_SESSION'
] =
array
();
040
041
if
(!
empty
(
$GLOBALS
[
'cookie_path'
]))
042
{
043
$this
->session_cookie_path =
$GLOBALS
[
'cookie_path'
];
044
}
045
else
046
{
047
$this
->session_cookie_path =
'/'
;
048
}
049
050
if
(!
empty
(
$GLOBALS
[
'cookie_domain'
]))
051
{
052
$this
->session_cookie_domain =
$GLOBALS
[
'cookie_domain'
];
053
}
054
else
055
{
056
$this
->session_cookie_domain =
''
;
057
}
058
059
if
(!
empty
(
$GLOBALS
[
'cookie_secure'
]))
060
{
061
$this
->session_cookie_secure =
$GLOBALS
[
'cookie_secure'
];
062
}
063
else
064
{
065
$this
->session_cookie_secure = false;
066
}
067
068
$this
->session_name =
$session_name
;
069
070
$this
->db = &
$db
;
071
$this
->_ip = real_ip();
072
073
if
(
$session_id
==
''
&& !
empty
(
$_COOKIE
[
$this
->session_name]))
074
{
075
$this
->session_id =
$_COOKIE
[
$this
->session_name];
076
}
077
else
078
{
079
$this
->session_id =
$session_id
;
080
}
081
082
if
(
$this
->session_id)
083
{
084
$tmp_session_id
=
substr
(
$this
->session_id, 0, 32);
085
if
(
$this
->gen_session_key(
$tmp_session_id
) ==
substr
(
$this
->session_id, 32))
086
{
087
$this
->session_id =
$tmp_session_id
;
088
}
089
else
090
{
091
$this
->session_id =
''
;
092
}
093
}
094
095
$this
->_time = time();
096
097
if
(
$this
->session_id)
098
{
099
$this
->load_session();
100
}
101
else
102
{
103
$this
->gen_session_id();
104
setcookie(
$this
->session_name,
$this
->session_id .
$this
->gen_session_key(
$this
->session_id), 0,
$this
->session_cookie_path,
$this
->session_cookie_domain,
$this
->session_cookie_secure);
105
}
106
register_shutdown_function(
array
(&
$this
,
'close_session'
));
107
}
108
109
function
gen_session_id()
110
{
111
$this
->session_id = md5(uniqid(mt_rand(), true));
112
113
return
$this
->insert_session();
114
}
115
116
function
gen_session_key(
$session_id
)
117
{
118
static
$ip
=
''
;
119
120
if
(
$ip
==
''
)
121
{
122
$ip
=
substr
(
$this
->_ip, 0,
strrpos
(
$this
->_ip,
'.'
));
123
}
124
125
return
sprintf(
'%08x'
, crc32(!
empty
(
$_SERVER
[
'HTTP_USER_AGENT'
]) ?
$_SERVER
[
'HTTP_USER_AGENT'
] . ROOT_PATH .
$ip
.
$session_id
: ROOT_PATH .
$ip
.
$session_id
));
126
}
127
128
function
insert_session()
129
{
130
return
$this
->db->set(
$this
->session_id,
array
(
'expiry'
=>
$this
->_time,
'ip'
=>
$this
->_ip,
'data'
=>
'a:0:{}'
), false,
$this
->max_life_time);
131
}
132
133
function
load_session()
134
{
135
$session
=
$this
->db->get(
$this
->session_id);
136
if
(
empty
(
$session
))
137
{
138
$this
->insert_session();
139
$this
->session_expiry = 0;
140
$this
->session_md5 =
'40cd750bba9870f18aada2478b24840a'
;
141
$GLOBALS
[
'_SESSION'
] =
array
();
142
}
143
else
144
{
145
if
(!
empty
(
$session
[
'data'
]) &&
$this
->_time -
$session
[
'expiry'
] <=
$this
->max_life_time)
146
{
147
$this
->session_expiry =
$session
[
'expiry'
];
148
$this
->session_md5 = md5(
$session
[
'data'
]);
149
$GLOBALS
[
'_SESSION'
] = unserialize(
stripslashes
(
$session
[
'data'
]));
150
}
151
else
152
{
153
$this
->session_expiry = 0;
154
$this
->session_md5 =
'40cd750bba9870f18aada2478b24840a'
;
155
$GLOBALS
[
'_SESSION'
] =
array
();
156
}
157
}
158
}
159
160
function
update_session()
161
{
162
$adminid
= !
empty
(
$GLOBALS
[
'_SESSION'
][
'admin_id'
]) ?
intval
(
$GLOBALS
[
'_SESSION'
][
'admin_id'
]) : 0;
163
$userid
= !
empty
(
$GLOBALS
[
'_SESSION'
][
'user_id'
]) ?
intval
(
$GLOBALS
[
'_SESSION'
][
'user_id'
]) : 0;
164
165
$data
= serialize(
$GLOBALS
[
'_SESSION'
]);
166
$this
->_time = time();
167
168
if
(
$this
->session_md5 == md5(
$data
) &&
$this
->_time <
$this
->session_expiry + 10)
169
{
170
return
true;
171
}
172
173
$data
=
addslashes
(
$data
);
174
175
return
$this
->db->replace(
$this
->session_id,
array
(
'expiry'
=>
$this
->_time,
'ip'
=>
$this
->_ip,
'userid'
=>
$userid
,
'adminid'
=>
$adminid
,
'data'
=>
$data
), false,
$this
->max_life_time);
176
}
177
178
function
close_session()
179
{
180
$this
->update_session();
181
return
true;
182
}
183
184
function
delete_spec_admin_session(
$adminid
)
185
{
186
if
(!
empty
(
$GLOBALS
[
'_SESSION'
][
'admin_id'
]) &&
$adminid
)
187
{
188
$all_items
=
$this
->db->getExtendedStats(
'items'
);
189
$items
=
$all_items
[
'127.0.0.1:11211'
][
'items'
];
190
foreach
(
$items
as
$key
=>
$item
) {
191
if
(isset(
$item
[
'adminid'
])) {
192
if
(
$item
[
'adminid'
] ==
$adminid
)
return
$this
->db->
delete
(
$key
);
193
}
194
}
195
}
196
else
197
{
198
return
false;
199
}
200
}
201
202
function
destroy_session()
203
{
204
$GLOBALS
[
'_SESSION'
] =
array
();
205
206
setcookie(
$this
->session_name,
$this
->session_id, 1,
$this
->session_cookie_path,
$this
->session_cookie_domain,
$this
->session_cookie_secure);
207
208
/* ECSHOP 自定义执行部分 */
209
if
(!
empty
(
$GLOBALS
[
'ecs'
]))
210
{
211
$GLOBALS
[
'db'
]->query(
'DELETE FROM '
.
$GLOBALS
[
'ecs'
]->table(
'cart'
) .
" WHERE session_id = '$this->session_id'"
);
212
}
213
/* ECSHOP 自定义执行部分 */
214
215
return
$this
->db->
delete
(
$this
->session_id);
216
}
217
218
function
get_session_id()
219
{
220
return
$this
->session_id;
221
}
222
223
function
get_users_count()
224
{
225
$all_items
=
$this
->db->getExtendedStats();
226
return
$count
=
$all_items
[
'127.0.0.1:11211'
][
'curr_items'
];
//由于有其他key的缓存,因此这只是个接近数值
227
}
228
229
}
230
231
?>
- 将ecshop中的session机制重写,从DB移植到Memcache中去
- 将ecshop中的session机制重写,从DB移植到Memcache中去
- 将session保存到memcache
- 将session信息写入到memcache中管理
- PHP高级配置 ---将session存到Memcache缓存中
- 怎样将ASPNETDB.MDF这个文件中的内容移植到Sql server 2000中去?
- 如何将session数据保存到memcache中,以及memcache的生存周期
- 如何将session数据保存到memcache中,以及memcache的生存周期
- 将session存到远程服务器中session过期机制
- 移植mongoDb中一个db中的collections到同一主机的另一db中
- PHP将session保存到memcache服务器
- java编程将excel中的数据存入DB或从DB导出到excel
- 将数据库中的数据取到memcache中
- ecshop的session放入memcache
- php将session写入memcache中。
- 从手册去理解PHP session机制
- db生成方法一:将assets中的数据库atf.db拷贝到内部存储中
- ThinkPHP中将session保存到memcache中
- JSTL概述
- perl unicode
- RSA python
- Altium Designer / Protel 发展历程
- jquery学习笔记---jqGrid学习笔记 完整整理
- 将ecshop中的session机制重写,从DB移植到Memcache中去
- ArrayList和LinkedList的区别
- phpcms v9 企业会员 选择企业库类型 无法注册
- 写宏注意
- 推荐:BlackBerry UI组件例子代码
- 光棍节 要吃 光棍面 《光棍面的制作方法》
- YII Framework学习教程-用YIIC快速创建YII应用之四-扩展yiic自定义命令-2011-11-11
- PHPCMS V9系统配置文件和模板目录
- 女友语录集