首先我们找到论坛首页网站模板的处理程序页面,source/module/forum/forum_index.php 找到如下代码:
- $onlineinfo = explode(" ", $_G['cache']['onlinerecord']);
- if(empty($_G['cookie']['onlineusernum'])) {
- $onlinenum = DB::result_first("SELECT count(*) FROM ".DB::table('common_session'));
- if($onlinenum > $onlineinfo[0]) {
- $onlinerecord = "$onlinenum ".TIMESTAMP;
- DB::query("UPDATE
".DB::table('common_setting')." SET svalue='$onlinerecord' WHERE
skey='onlinerecord'"); - save_syscache('onlinerecord', $onlinerecord);
- $onlineinfo = array($onlinenum, TIMESTAMP);
- }
- dsetcookie('onlineusernum', intval($onlinenum), 300);
- } else {
- $onlinenum = intval($_G['cookie']['onlineusernum']);
- }
这里$onlineinfo 为在线信息的缓存记录,如果此值为空,则查询 common_session 表,读取在线人数的缓存。如果当前人数大于缓存最高在线人数,则更新 common_setting 表的最高在线记录人数,并缓存。如果有在线人数记录则直接读取在线人数。
如果后台开启了现实在线人数,则处理在线现会员信息。
- $_G['setting']['maxonlinelist'] = $_G['setting']['maxonlinelist'] ? $_G['setting']['maxonlinelist'] : 500;
在此程序对默认显示的在线人数默认限定为500;
- $query = DB::query("SELECT uid, username, groupid, invisible, lastactivity, fid FROM ".DB::table('common_session')." WHERE uid>'0' LIMIT ".$_G['setting']['maxonlinelist']);
- while($online = DB::fetch($query)) {
- $membercount ++;
- if($online['invisible']) {
- $invisiblecount++;
- continue;
- } else {
- $online['icon']
= !empty($_G['cache']['onlinelist'][$online['groupid']]) ?
$_G['cache']['onlinelist'][$online['groupid']] :
$_G['cache']['onlinelist'][0]; - }
- $online['lastactivity'] = dgmdate($online['lastactivity'], 't');
- $whosonline[] = $online;
- }
这里通过 common_session 表的查询,搜索出最近在线的显示会员,这里排除了 invisible 的隐身会员。
- if(isset($_G['cache']['onlinelist'][7]) && $_G['setting']['maxonlinelist'] > $membercount) {
- $query = DB::query("SELECT uid,
username, groupid, invisible, lastactivity, fid FROM
".DB::table('common_session')." WHERE uid='0' ORDER BY uid DESC LIMIT
".($_G['setting']['maxonlinelist'] - $membercount)); - while($online = DB::fetch($query)) {
- $online['icon'] = $_G['cache']['onlinelist'][7];
- $online['username'] = $_G['cache']['onlinelist']['guest'];
- $online['lastactivity'] = dgmdate($online['lastactivity'], 't');
- $whosonline[] = $online;
- }
- }
这里如果在最大在线人数大于当前在线会员,这剩余的部分通过游客补全。
- if($onlinenum > $_G['setting']['maxonlinelist']) {
- $membercount = $discuz->session->onlinecount(1);
- $invisiblecount =
DB::result_first("SELECT COUNT(*) FROM ".DB::table('common_session')."
WHERE invisible = '1'"); - }
- if($onlinenum < $membercount) {
- $onlinenum = $discuz->session->onlinecount(0);
- dsetcookie('onlineusernum', intval($onlinenum), 300);
- }
- $guestcount = $onlinenum - $membercount;
以上代码的意思是,如果在线人数> 最大显示在线人数,则重新统计在线会员人数。
一旦在线人数< 在线会员数,则重新统计所有在线会员数 (包括隐身) 并写入缓存。
最后由在线总人数和在线会员数得出在线游客。