php实现统计IP数及在线人数的示例代码

 更新时间:2020年7月22日 13:03  点击:1705

写在前面的话

很多人有这样的需求,就是统计网站访问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("&nbsp;");";
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 统计IP数及在线人数内容请搜索猪先飞以前的文章或继续浏览下面的相关文章希望大家以后多多支持猪先飞!

[!--infotagslink--]

相关文章

  • 源码分析系列之json_encode()如何转化一个对象

    这篇文章主要介绍了源码分析系列之json_encode()如何转化一个对象,对json_encode()感兴趣的同学,可以参考下...2021-04-22
  • php中去除文字内容中所有html代码

    PHP去除html、css样式、js格式的方法很多,但发现,它们基本都有一个弊端:空格往往清除不了 经过不断的研究,最终找到了一个理想的去除html包括空格css样式、js 的PHP函数。...2013-08-02
  • index.php怎么打开?如何打开index.php?

    index.php怎么打开?初学者可能不知道如何打开index.php,不会的同学可以参考一下本篇教程 打开编辑:右键->打开方式->经文本方式打开打开运行:首先你要有个支持运行PH...2017-07-06
  • PHP中func_get_args(),func_get_arg(),func_num_args()的区别

    复制代码 代码如下:<?php function jb51(){ print_r(func_get_args()); echo "<br>"; echo func_get_arg(1); echo "<br>"; echo func_num_args(); } jb51("www","j...2013-10-04
  • php精确的统计在线人数的方法

    这是一个非常精确的,通过php实现统计在线人数的方法,想知道怎么实现的请耐心阅读。<&#63;php $filename='online.txt';//数据文件 $cookiename='VGOTCN_OnLineCount';//cookie名称 $onlinetime=600;//在线有效时间,单位:...2015-10-23
  • PHP编程 SSO详细介绍及简单实例

    这篇文章主要介绍了PHP编程 SSO详细介绍及简单实例的相关资料,这里介绍了三种模式跨子域单点登陆、完全跨单点域登陆、站群共享身份认证,需要的朋友可以参考下...2017-01-25
  • PHP实现创建以太坊钱包转账等功能

    这篇文章主要介绍了PHP实现创建以太坊钱包转账等功能,对以太坊感兴趣的同学,可以参考下...2021-04-20
  • php微信公众账号开发之五个坑(二)

    这篇文章主要为大家详细介绍了php微信公众账号开发之五个坑,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2016-10-02
  • PHP如何通过date() 函数格式化显示时间

    这篇文章主要介绍了PHP如何通过date() 函数格式化显示时间,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-11-13
  • ThinkPHP使用心得分享-ThinkPHP + Ajax 实现2级联动下拉菜单

    首先是数据库的设计。分类表叫cate.我做的是分类数据的二级联动,数据需要的字段有:id,name(中文名),pid(父id). 父id的设置: 若数据没有上一级,则父id为0,若有上级,则父id为上一级的id。数据库有内容后,就可以开始写代码,进...2014-05-31
  • golang与php实现计算两个经纬度之间距离的方法

    这篇文章主要介绍了golang与php实现计算两个经纬度之间距离的方法,结合实例形式对比分析了Go语言与php进行经纬度计算的相关数学运算技巧,需要的朋友可以参考下...2016-07-29
  • PHP正则表达式过滤html标签属性(DEMO)

    这篇文章主要介绍了PHP正则表达式过滤html标签属性的相关内容,实用性非常,感兴趣的朋友参考下吧...2016-05-06
  • php构造方法中析构方法在继承中的表现

    这篇文章主要为大家详细介绍了php构造方法中析构方法在继承中的表现,感兴趣的小伙伴们可以参考一下...2016-04-15
  • PHP如何使用cURL实现Get和Post请求

    这篇文章主要介绍了PHP如何使用cURL实现Get和Post请求,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-07-11
  • PHP+jQuery+Ajax实现多图片上传效果

    今天我给大家分享的是在不刷新页面的前提下,使用PHP+jQuery+Ajax实现多图片上传的效果。用户只需要点击选择要上传的图片,然后图片自动上传到服务器上并展示在页面上。...2015-03-15
  • thinkPHP中多维数组的遍历方法

    这篇文章主要介绍了thinkPHP中多维数组的遍历方法,以简单实例形式分析了thinkPHP中foreach语句的使用技巧,需要的朋友可以参考下...2016-01-12
  • PHP简单实现生成txt文件到指定目录的方法

    这篇文章主要介绍了PHP简单实现生成txt文件到指定目录的方法,简单对比分析了PHP中fwrite及file_put_contents等函数的使用方法,需要的朋友可以参考下...2016-04-28
  • php图片添加文字水印实现代码

    这篇文章主要为大家详细介绍了php图片添加文字水印实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2016-03-17
  • php判断邮箱地址是否存在的方法

    这篇文章主要介绍了php判断邮箱地址是否存在的方法,php判断邮箱地址是否存在的方法有两种,感兴趣的朋友可以参考一下...2016-02-18
  • thinkphp自定义权限管理之名称判断方法

    下面小编就为大家带来一篇thinkphp自定义权限管理之名称判断方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2017-04-03