php怎样在线人数统计和用户IP数?
Admin 2021-04-25 群英技术资讯 1800 次浏览
我们在访问一些网站的时候,常常会看到在线人数是多少。其实通过统计用户IP数以及在线人数,不仅能够帮助管理人员了解用户的访问和使用情况,而且展示网站在线人数,在某种程度上能够给用户一种认同感。那么PHP是如何实现统计IP 和在线人数的呢?下面是具体的代码。
开启依赖函数模块
实现这个功能,需要依赖putenv()函数。下面两种方式均可。
更改php.ini文件方法
找到php.ini文件,搜索putenv关键字,删除即可。
isable_functions = passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,
ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,
pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,
pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,
pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv
使用宝塔面板
点击左侧软件管理,找到php,然后设置->禁用函数。

删除putenv,然后重启php即可。
实现函数
在count.php同目录下创建文件:count,temp,online。新建文本文档count.txt,去掉扩展名即为count了;
linux系统中请设置文件属性为:777。
文件count.php代码,用到了php函数--explode、isset、empty及sprintf等:
<?php
$file = "count"; // 记数文件名称
$startno = "1000"; // 起始数值
$tempfile = "temp";
$t_now = time();
$t_array = getdate($t_now);
$day = $t_array['mday'];
$mon = $t_array['mon'];
$year = $t_array['year'];
if (file_exists("$file")) {
$count_info=file("$file");
$c_info = explode(",", $count_info[0]);
$total_c=$c_info[0];
$yesterday_c=$c_info[1];
$today_c=$c_info[2];
$lastday=$c_info[3];
} else {
$total_c="$startno";
$yesterday_c="0";
$today_c="0";
$lastday="0";
}
if ( !isset($HTTP_COOKIE_VARS["countcookie"]) || $HTTP_COOKIE_VARS["countcookie"] != $day) {
$your_c=1;
$lockfile=fopen("temp","a");
flock($lockfile,3);
putenv('TZ=JST-9');
$t_array2 = getdate($t_now-24*3600);
$day2=$t_array2['mday'];
$mon2=$t_array2['mon'];
$year2=$t_array2['year'];
$today = "$year-$mon-$day";
$yesterday = "$year2-$mon2-$day2";
if ($today != $lastday) {
if ($yesterday != $lastday) $yesterday_c = "0";
else $yesterday_c = $today_c;
$today_c = 0;
$lastday = $today;
}
$total_c++;
$today_c++;
$total_c = sprintf("%06d", $total_c);
$today_c = sprintf("%06d", $today_c);
$yesterday_c = sprintf("%06d", $yesterday_c);
setcookie("countcookie","$day",$t_now+43200);
$fp=fopen("$file","w");
fputs($fp, "$total_c,$yesterday_c,$today_c,$lastday");
fclose($fp);
fclose($lockfile);
}
if ( empty( $your_c ) ) $your_c = 1;
setcookie("yourcount",$your_c+1,$t_now+43200);
$your_c = sprintf("%06d", $your_c);
//////////////////////////开始统计在线
$filename="online";
$onlinetime=600; //同一IP在线时间,单位:秒
$online_id=file($filename);
$total_online=count($online_id);
$ip=getenv("REMOTE_ADDR");
$nowtime=time();
for($i=0;$i<$total_online;$i++){
$oldip=explode("||",$online_id[$i]);
$hasonlinetime=$nowtime-$oldip[0];
if($hasonlinetime<$onlinetime and $ip!=$oldip[1]) $nowonline[]=$online_id[$i];
}
$nowonline[]=$nowtime."||".$ip."||";
$total_online=count($nowonline);
$fp=fopen($filename,"w");
rewind($fp);
for($i=0;$i<$total_online;$i++){
fputs($fp,$nowonline[$i]);
fputs($fp,"n");
}
fclose($fp);
if($total_online==0)$total_online=1;
$total_online = sprintf("%06d", $total_online);
///////////////////////////////////////////////////////
echo "document.write("・总IP访问:".$total_c."");";
echo "document.write("<br>");";
echo "document.write("・昨日访问:".$yesterday_c."");";
echo "document.write("<br>");";
echo "document.write("今日IP:".$today_c."");";
echo "document.write(" ");";
echo "document.write("・您 访 问:".$your_c."");";
echo "document.write("<br>");";
echo "document.write("当前在线:".$total_online."");";
exit;
?>

调用
用JS调用文件count.php
在需要加入统计的的地方,添加:
<script src="/php/count.php"></script>
以上就是php实现统计IP数及在线人数的代码展示,有需要的朋友能够多看看,希望对大家学习PHP有帮助,更多PHP相关内容大家可以关注其他文章。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
laravel框架是PHP中比较常用的框架,有很多新手在学习PHP时会遇到一些laravel框架相关的故障问题不知道怎么样解决,下面小编就给大家介绍一下常见的故障问题以及解决办法。
PHP实现按照大小进行函数排列的操作有哪些?在实际项目的操作过程或是学习过程中,不少人都会遇到这样的问题,接下来就让小编带大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
本篇文章小编给大家分享一下Python中re.findall()用法代码解析,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
laravel排序失效的解决办法:1、通过“$query->whereIn(...)”查询数据;2、通过filter过滤数据;3、设置排序好的数据为“$data = $scoutModelsLists;”。
在PHP中foreach循环语句,常用于遍历数组,一般有两种使用方式:不取下标、取下标(一)只取值,不取下标<?phpforeach(数组as值){//执行的任务}?>(二)同时取下标和值<?phpforeach(数组as下标=>值){//执行的任务}?>例子:<?php$students=array(
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008