3 1366 2021-02-13 11:59:20
收藏统计网站访问IP以及在线的人数,推荐使用成熟的统计程序来完成,下面这篇适合局域网或小型dome使用,实现简单小巧的php统计在线人数和IP功能。
首先我们需要开启php依赖putenv()函数模块,找到php.ini文件,搜索putenv关键字,删除即可。
如果你使用的宝塔面板环境,点击左侧软件管理,找到php,然后设置->禁用函数。
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
删除putenv,然后重启php即可。
下面是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; ?>
本文地址:https://xzo.com.cn/develop/php/542.html
如果您认可我们的分享,有意与我们合作开展帝国cms网站建设与开发业务或插件定制,请联系右侧在线客服。我们能给您的,就是高质量的模板与售后。
标注了信息来源为下载鸥的文章皆为原创,如果是转载的优质文章,我们也都标注了出处。如果您喜欢我们的文章,请按照下载鸥所标注的文章出处进行标注,谢谢您的配合。
PHP是一种服务器端、跨平台、html嵌入式的脚本语言,其功能十分强大、入门简单。在过去,民间一直有说法称“PHP是最好的编程语言”,是PHP开发者对自身所使用语言的肯定。
PHP曾是最主流的网站web开发语言之一,现在也依然十分流行。
php判断时间戳是不是今天的实现方法:首先创建一个PHP示例文件;指定一个时间,并...
互联网上每天都有很多爬虫到处爬取内容,你可能不知道他们是干嘛的,但其中肯定...
页面提交post请求后被宝塔防火墙拦截,查看日志显示错误:参数值长度超过20w已...
数组在实际开发过程中运用非常频繁,通过合理运用数组,我们可以顺利实现各种数...
在一些开发场景中,我们需要在每次发起请求后更新重置session_id以同步用户数...
json是使用非常广泛的数据,通过使用json,我们还可以实现多种语言的联动。今天...
isset() 函数用于检测变量是否已设置并且非 NULL。如果已经使用 unset() 释...
今天有朋友咨询鸥哥,说php写的for循环本来至少要循环9次,但却只执行了一次就...
在用php实现无刷新在线点赞功能前,我们需要知道这个功能逻辑流程是什么,其实...
大家都知道CC攻击一般攻击网站的动态页面,静态页面一般随便用个防御软件就可...
exit() 函数输出一条消息,并退出当前脚本。通俗解释就是终止当前脚本,exit()...
简单的PHP域名授权访问代码,增加了Zend 5加密,本地验证。通过程序限制域名从...
当网站图片尺寸过大、图片过多的情况下,会引起页面加载缓慢的问题。人工手动...
PHP中,终止脚本运行有三种方式:主脚本程序中使用return、脚本中使用exit()、d...
在php的开发中,我们常常需要使用到header函数头来进行做标记...
都知道PHP无非就是增删改查,学会了增删改查就入门了PHP,精准像素分享一篇PHP...