将ecshop中的session机制重写,从DB移植到Memcache中去
来源:互联网 发布:王震对新疆的功过知乎 编辑:程序博客网 时间:2024/04/19 03:56
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
<?php
if
(!defined(
'IN_ECS'
))
{
die
(
'Hacking attempt'
);
}
/*------------------------------------------------------ */
//-- 该类用于将SESSION直接写入Memcache
/*------------------------------------------------------ */
class
cls_session
{
var
$db
= NULL;
var
$max_life_time
= 1800;
// SESSION 过期时间
var
$session_name
=
''
;
var
$session_id
=
''
;
var
$session_expiry
=
''
;
var
$session_md5
=
''
;
var
$session_cookie_path
=
'/'
;
var
$session_cookie_domain
=
''
;
var
$session_cookie_secure
= false;
var
$_ip
=
''
;
var
$_time
= 0;
function
__construct(&
$db
,
$session_table
,
$session_data_table
,
$session_name
=
'ECS_ID'
,
$session_id
=
''
)
{
$m
=
new
Memcache;
$m
->addServer(
'127.0.0.1'
, 11211);
$this
->cls_session(
$m
,
$session_name
,
$session_id
);
}
function
cls_session(&
$db
,
$session_name
=
'ECS_ID'
,
$session_id
=
''
)
{
$GLOBALS
[
'_SESSION'
] =
array
();
if
(!
empty
(
$GLOBALS
[
'cookie_path'
]))
{
$this
->session_cookie_path =
$GLOBALS
[
'cookie_path'
];
}
else
{
$this
->session_cookie_path =
'/'
;
}
if
(!
empty
(
$GLOBALS
[
'cookie_domain'
]))
{
$this
->session_cookie_domain =
$GLOBALS
[
'cookie_domain'
];
}
else
{
$this
->session_cookie_domain =
''
;
}
if
(!
empty
(
$GLOBALS
[
'cookie_secure'
]))
{
$this
->session_cookie_secure =
$GLOBALS
[
'cookie_secure'
];
}
else
{
$this
->session_cookie_secure = false;
}
$this
->session_name =
$session_name
;
$this
->db = &
$db
;
$this
->_ip = real_ip();
if
(
$session_id
==
''
&& !
empty
(
$_COOKIE
[
$this
->session_name]))
{
$this
->session_id =
$_COOKIE
[
$this
->session_name];
}
else
{
$this
->session_id =
$session_id
;
}
if
(
$this
->session_id)
{
$tmp_session_id
=
substr
(
$this
->session_id, 0, 32);
if
(
$this
->gen_session_key(
$tmp_session_id
) ==
substr
(
$this
->session_id, 32))
{
$this
->session_id =
$tmp_session_id
;
}
else
{
$this
->session_id =
''
;
}
}
$this
->_time = time();
if
(
$this
->session_id)
{
$this
->load_session();
}
else
{
$this
->gen_session_id();
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);
}
register_shutdown_function(
array
(&
$this
,
'close_session'
));
}
function
gen_session_id()
{
$this
->session_id = md5(uniqid(mt_rand(), true));
return
$this
->insert_session();
}
function
gen_session_key(
$session_id
)
{
static
$ip
=
''
;
if
(
$ip
==
''
)
{
$ip
=
substr
(
$this
->_ip, 0,
strrpos
(
$this
->_ip,
'.'
));
}
return
sprintf(
'%08x'
, crc32(!
empty
(
$_SERVER
[
'HTTP_USER_AGENT'
]) ?
$_SERVER
[
'HTTP_USER_AGENT'
] . ROOT_PATH .
$ip
.
$session_id
: ROOT_PATH .
$ip
.
$session_id
));
}
function
insert_session()
{
return
$this
->db->set(
$this
->session_id,
array
(
'expiry'
=>
$this
->_time,
'ip'
=>
$this
->_ip,
'data'
=>
'a:0:{}'
), false,
$this
->max_life_time);
}
function
load_session()
{
$session
=
$this
->db->get(
$this
->session_id);
if
(
empty
(
$session
))
{
$this
->insert_session();
$this
->session_expiry = 0;
$this
->session_md5 =
'40cd750bba9870f18aada2478b24840a'
;
$GLOBALS
[
'_SESSION'
] =
array
();
}
else
{
if
(!
empty
(
$session
[
'data'
]) &&
$this
->_time -
$session
[
'expiry'
] <=
$this
->max_life_time)
{
$this
->session_expiry =
$session
[
'expiry'
];
$this
->session_md5 = md5(
$session
[
'data'
]);
$GLOBALS
[
'_SESSION'
] = unserialize(
stripslashes
(
$session
[
'data'
]));
}
else
{
$this
->session_expiry = 0;
$this
->session_md5 =
'40cd750bba9870f18aada2478b24840a'
;
$GLOBALS
[
'_SESSION'
] =
array
();
}
}
}
function
update_session()
{
$adminid
= !
empty
(
$GLOBALS
[
'_SESSION'
][
'admin_id'
]) ?
intval
(
$GLOBALS
[
'_SESSION'
][
'admin_id'
]) : 0;
$userid
= !
empty
(
$GLOBALS
[
'_SESSION'
][
'user_id'
]) ?
intval
(
$GLOBALS
[
'_SESSION'
][
'user_id'
]) : 0;
$data
= serialize(
$GLOBALS
[
'_SESSION'
]);
$this
->_time = time();
if
(
$this
->session_md5 == md5(
$data
) &&
$this
->_time <
$this
->session_expiry + 10)
{
return
true;
}
$data
=
addslashes
(
$data
);
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);
}
function
close_session()
{
$this
->update_session();
return
true;
}
function
delete_spec_admin_session(
$adminid
)
{
if
(!
empty
(
$GLOBALS
[
'_SESSION'
][
'admin_id'
]) &&
$adminid
)
{
$all_items
=
$this
->db->getExtendedStats(
'items'
);
$items
=
$all_items
[
'127.0.0.1:11211'
][
'items'
];
foreach
(
$items
as
$key
=>
$item
) {
if
(isset(
$item
[
'adminid'
])) {
if
(
$item
[
'adminid'
] ==
$adminid
)
return
$this
->db->
delete
(
$key
);
}
}
}
else
{
return
false;
}
}
function
destroy_session()
{
$GLOBALS
[
'_SESSION'
] =
array
();
setcookie(
$this
->session_name,
$this
->session_id, 1,
$this
->session_cookie_path,
$this
->session_cookie_domain,
$this
->session_cookie_secure);
/* ECSHOP 自定义执行部分 */
if
(!
empty
(
$GLOBALS
[
'ecs'
]))
{
$GLOBALS
[
'db'
]->query(
'DELETE FROM '
.
$GLOBALS
[
'ecs'
]->table(
'cart'
) .
" WHERE session_id = '$this->session_id'"
);
}
/* ECSHOP 自定义执行部分 */
return
$this
->db->
delete
(
$this
->session_id);
}
function
get_session_id()
{
return
$this
->session_id;
}
function
get_users_count()
{
$all_items
=
$this
->db->getExtendedStats();
return
$count
=
$all_items
[
'127.0.0.1:11211'
][
'curr_items'
];
//由于有其他key的缓存,因此这只是个接近数值
}
}
?>
0 0
- 将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中
- 使用Dijkstra迪杰斯特拉算法获得单源最短路径
- MYSQL [Warning] Aborted connection 42355 to db
- eclipse juno(4.2) 集成 maven 插件
- Scala 学习笔记(4)-集合类简单操作
- python函数转模块命名
- 将ecshop中的session机制重写,从DB移植到Memcache中去
- 为什么牛顿法下降的速度比梯度下降的快
- OC中方法与函数的区别
- OAuth 1.0 协议整理
- 黑马程序员——Foundation框架
- jogl中使用顶点数组
- Hadoop中的一些基本操作
- 内部插入排序---折半插入排序
- IOS 圆形头像 UIButton边框 按钮边框