Typecho-Joe主题新增在线人数统计功能

无心博客
2025-05-26 / 0 评论 / 26 阅读 / 正在检测是否收录...

成品如图
c5b2fc5356992bd5af57af8c2d87dae81364379179217efdac0854e28398b893.0.PNG
613011973165c5e5ba34cd7c035863ab9e748ca87fb46dade67c1bdfbe7b6bdb.0.PNG

1.修改functions.php,底部新增,文件路径:usr/themes/Joe;如果无法显示设置,则将以下joe_custom修改为joe_other

// 在线人数设置
$onlinePersion = new Typecho_Widget_Helper_Form_Element_Select(
    'onlinePersion',
    array(
        'on' => '开启(默认)',
        'off' => '关闭',
    ),
    'on',
    '是否启用在线人数统计',
    '介绍:开启后,文章底部展示当前在线人数'
);
$onlinePersion->setAttribute('class', 'joe_content joe_custom');
$form->addInput($onlinePersion->multiMode());

9b143e9d1ccd2fa3a9956a7b09b47129104717e772e4eedd4a2ea9326938b9ce.0.PNG
2.修改footer.php文件,文件路径:/usr/themes/Joe/public

①第一段,顶部新增

<?php
    //首先你要有读写文件的权限,首次访问不显示,正常情况刷新即可
    $online_log = "slzxrs.dat"; //保存人数的文件到根目录,
    $timeout = 30;//30秒内没动作者,认为掉线
    $entries = file($online_log);
    $temp = array();
    for ($i=0;$i<count($entries);$i++){
        $entry = explode(",",trim($entries[$i]));
        if(($entry[0] != getenv('REMOTE_ADDR')) && ($entry[1] > time())) {
            array_push($temp,$entry[0].",".$entry[1]."\n"); //取出其他浏览者的信息,并去掉超时者,保存进$temp
        }
    }
    array_push($temp,getenv('REMOTE_ADDR').",".(time() + ($timeout))."\n"); //更新浏览者的时间
    $slzxrs = count($temp); //计算在线人数
    $entries = implode("",$temp);
    //写入文件
    $fp = fopen($online_log,"w");
    flock($fp,LOCK_EX); //flock() 不能在NFS以及其他的一些网络文件系统中正常工作
    fputs($fp,$entries);
    flock($fp,LOCK_UN);
    fclose($fp);
    $tj= "在线人数:".$slzxrs."人";
?>

②第二段,中间新增

<div class="item" style="margin: 10px; <?php if(Helper::options()->onlinePersion !== 'on') echo 'display:none;'  ?>">
    <?php echo $tj ?>
</div>

4f77f556c2f56398934b679e634e2bc7224152547df9b0052a25ee804a8e03a3.0.PNG
1ec3fc92bd5b8a5d52429cd47ebba6b9bf0125884c62bba071950ee040bc3103.0.PNG

1

评论 (0)

取消