discuzx在线人数设置

  核心提示:常有碰到有同学问在线人数的问题,官方默认的是显示最大在线500人(建议不要改动太大的值,影响页面加载的效率)。这里我就和大家一起看看,在线人数是如何设置和现实的。首先我们找到论坛首页网站模板的处理程序页面,source/module/forum/forum_index.php找到如下代码:$onli…

常有碰到有同学问在线人数的问题,官方默认的是显示最大在线500人(建议不要改动太大的值,影响页面加载的效率)。这里我就和大家一起看看,在线人数是如何设置和现实的。
首先我们找到论坛首页网站模板的处理程序页面,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;
以上代码的意思是,如果在线人数>最大显示在线人数,则重新统计在线会员人数。

一旦在线人数<在线会员数,则重新统计所有在线会员数(包括隐身)并写入缓存。

最后由在线总人数和在线会员数得出在线游客。

未经允许不得转载:445IT之家 » discuzx在线人数设置

赞 (0) 打赏

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏