【discuzX2】/source/class/class_core.php文件中session会话类discuz_session分析

  1. <?php  
  2. //session类,session机制,discuz中session信息存储在数据库中,而不是存储在硬盘上  
  3. class discuz_session {  
  4.   
  5.     var $sid = null;  
  6.     var $var;  
  7.     var $isnew = false;  
  8.         //初始化session数组  
  9.     var $newguest = array('sid' => 0, 'ip1' => 0, 'ip2' => 0, 'ip3' => 0, 'ip4' => 0,  
  10.     'uid' => 0, 'username' => '', 'groupid' => 7, 'invisible' => 0, 'action' => 0,  
  11.     'lastactivity' => 0, 'fid' => 0, 'tid' => 0, 'lastolupdate' => 0);  
  12.   
  13.     var $old =  array('sid' =>  '', 'ip' =>  '', 'uid' =>  0);  
  14.   
  15.     function discuz_session($sid = '', $ip = '', $uid = 0) {  
  16.         $this->old = array('sid' =>  $sid, 'ip' =>  $ip, 'uid' =>  $uid);  
  17.         $this->var = $this->newguest;  
  18.         if(!empty($ip)) {  
  19.             $this->init($sid, $ip, $uid);  
  20.         }  
  21.     }  
  22.   
  23.         //设置  
  24.     function set($key, $value) {  
  25.         if(isset($this->newguest[$key])) {  
  26.             $this->var[$key] = $value;  
  27.         } elseif ($key == 'ip') {  
  28.             $ips = explode('.', $value);  
  29.             $this->set('ip1', $ips[0]);  
  30.             $this->set('ip2', $ips[1]);  
  31.             $this->set('ip3', $ips[2]);  
  32.             $this->set('ip4', $ips[3]);  
  33.         }  
  34.     }  
  35.   
  36.         //获取  
  37.     function get($key) {  
  38.         if(isset($this->newguest[$key])) {  
  39.             return $this->var[$key];  
  40.         } elseif ($key == 'ip') {  
  41.             return $this->get('ip1').'.'.$this->get('ip2').'.'.$this->get('ip3').'.'.$this->get('ip4');  
  42.         }  
  43.     }  
  44.   
  45.         //初始化  
  46.     function init($sid, $ip, $uid) {  
  47.         $this->old = array('sid' =>  $sid, 'ip' =>  $ip, 'uid' =>  $uid);  
  48.         $session = array();  
  49.         if($sid) {  
  50.             $session = DB::fetch_first("SELECT * FROM ".DB::table('common_session').  
  51.                 " WHERE sid='$sid' AND CONCAT_WS('.', ip1,ip2,ip3,ip4)='$ip'");  
  52.         }  
  53.   
  54.         if(empty($session) || $session['uid'] != $uid) {  
  55.             $session = $this->create($ip, $uid);  
  56.         }  
  57.   
  58.         $this->var = $session;  
  59.         $this->sid = $session['sid'];  
  60.     }  
  61.   
  62.         //创建  
  63.     function create($ip, $uid) {  
  64.   
  65.         $this->isnew = true;  
  66.         $this->var = $this->newguest;  
  67.         $this->set('sid', random(6));  
  68.         $this->set('uid', $uid);  
  69.         $this->set('ip', $ip);  
  70.         $uid && $this->set('invisible', getuserprofile('invisible'));  
  71.         $this->set('lastactivity', time());  
  72.         $this->sid = $this->var['sid'];  
  73.   
  74.         return $this->var;  
  75.     }  
  76.   
  77.         //删除  
  78.     function delete() {  
  79.   
  80.         global $_G;  
  81.         $onlinehold = $_G['setting']['onlinehold'];  
  82.         $guestspan = 60;  
  83.   
  84.         $onlinehold = time() - $onlinehold;  
  85.         $guestspan = time() - $guestspan;  
  86.   
  87.         $condition = " sid='{$this->sid}' ";  
  88.         $condition .= " OR lastactivity<$onlinehold ";  
  89.         $condition .= " OR (uid='0' AND ip1='{$this->var['ip1']}' AND ip2='{$this->var['ip2']}' AND ip3='{$this->var['ip3']}' AND ip4='{$this->var['ip4']}' AND lastactivity>$guestspan) ";  
  90.         $condition .= $this->var['uid'] ? " OR (uid='{$this->var['uid']}') " : '';  
  91.         DB::delete('common_session', $condition);  
  92.     }  
  93.   
  94.         //更新数据  
  95.     function update() {  
  96.         global $_G;  
  97.         if($this->sid !== null) {  
  98.   
  99.             $data = daddslashes($this->var);  
  100.             if($this->isnew) {  
  101.                 $this->delete();  
  102.                 DB::insert('common_session', $data, false, false, true);  
  103.             } else {  
  104.                 DB::update('common_session', $data, "sid='$data[sid]'");  
  105.             }  
  106.             $_G['session'] = $data;  
  107.             dsetcookie('sid', $this->sid, 86400);  
  108.         }  
  109.     }  
  110.   
  111.         /** 
  112.      * 取在线用户数量 
  113.      * 
  114.      * @param int $type 0=全部 1=会员 2=游客 
  115.      * @return int 
  116.      */  
  117.     function onlinecount($type = 0) {  
  118.         $condition = $type == 1 ? ' WHERE uid>0 ' : ($type == 2 ? ' WHERE invisible=1 ' : '');  
  119.         return DB::result_first("SELECT count(*) FROM ".DB::table('common_session').$condition);  
  120.     }  
  121.   
  122. }  
  123. ?>  
原文地址:https://www.cnblogs.com/alleyonline/p/7498501.html