首页 > 文章 > 杂文

将ecshop中的session从mysql移植到memcache的方法
时间:2013-12-02 14:41:51  点击: 来源:  作者:

[代码] [PHP]代码http://www.oschina.net/code/snippet_83895_6963 001 <?php 002 003 if (!defined(&#39;IN_ECS&#39;)) 004 { 005 die(&#39;Hacking attempt&#39;); 006 } 007 0 ...

[代码] [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     functioncls_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     functiongen_session_id()
110     {
111         $this->session_id = md5(uniqid(mt_rand(), true));
112  
113         return$this->insert_session();
114     }
115  
116     functiongen_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         returnsprintf('%08x', crc32(!empty($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] . ROOT_PATH .$ip . $session_id: ROOT_PATH . $ip. $session_id));
126     }
127  
128     functioninsert_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     functionload_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     functionupdate_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             returntrue;
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     functionclose_session()
179     {
180         $this->update_session();
181         returntrue;
182     }
183  
184     functiondelete_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             returnfalse;
199         }
200     }
201  
202     functiondestroy_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     functionget_session_id()
219     {
220         return$this->session_id;
221     }
222  
223     functionget_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 ?>
”您可通过以下微信二维码,赞赏作者“
 
 
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
推荐资讯
windows如何挂载webdav?
windows如何挂载webda
linux服务器同步互联网时间
linux服务器同步互联
亚马逊云lightsail服务器使用教程 | 亚马逊云lightsail使用指南
亚马逊云lightsail服
hostdare 4折优惠,直接降价60% 美国洛杉矶低至 $10.4/年 768M内存/1核/10gNVMe/500g流量
hostdare 4折优惠,直接
相关文章
栏目更新
栏目热门

关于我们 | 广告服务 | 联系我们 | 网站地图 | 免责声明 | WAP | RSS


Copyright © 运维之家 2013-2024