去评论
dz插件网

实现当前页面多少人在线多少访问量

婷姐
2025/01/04 08:51:09
理论上不分CMS与什么框架的。前提是要安装workerman.
以帝国CMS为例。非帝国CMS的把帝国的引用文件删除即可
  1. <?phpdefine('ECMS_PATH', substr(dirname(__FILE__), 0, -31)); // 程序所在目录require ECMS_PATH.'e/class/connect.php'; //引入帝国CMS数据库配置文件和公共函数文件require_once ECMS_PATH.'e/extend/workermanchat/Applications/GatewayClient/Gateway.php'; //引入推送文件use GatewayClient\Gateway;Gateway::$registerAddress = '127.0.0.1:1236';function recordOnlineUser($userIdentifier) {global $redis;$redis->zAdd('online_users_tongji', time(), $userIdentifier);}function countOnlineUsers() {global $redis;return $redis->zCard('online_users_tongji');}function recordPageVisit($pageUrl) {global $redis;$redis->incr('page_visits_tongji:'. $pageUrl);}function cleanExpiredOnlineUsers() {global $redis;$expiredTime = time() - 60;$redis->zRemRangeByScore('online_users_tongji', 0, $expiredTime);}function getPageVisits($pageUrl) {global $redis;return (int) $redis->get('page_visits_tongji:'. $pageUrl);}$userIdentifier =  uniqid(); recordOnlineUser($userIdentifier);$pageUrl = $_SERVER['REQUEST_URI']; recordPageVisit($pageUrl);$onlineCount = countOnlineUsers();$pageVisitCount = getPageVisits($pageUrl); $req_data['content']="当前在线人数:{$onlineCount},访问量:{$pageVisitCount}";$req_data['pageUrl']=$pageUrl;$req_data['onlineCount']=$onlineCount;$req_data['pageVisitCount']=$pageVisitCount;cleanExpiredOnlineUsers();$req_data['type'] = 'tongji_all';Gateway::sendToAll($req_data['content']);