当前位置:首页 > PHP教程 > php高级应用 > 列表

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

发布:smiling 来源: PHP粉丝网  添加日期:2022-03-19 15:04:47 浏览: 评论:0 

这篇文章主要介绍了php实现统计IP数及在线人数的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。

写在前面的话

很多人有这样的需求,就是统计网站访问IP以及在线的人数,今天我们就看一下具体实现方法。

开启依赖函数模块

实现这个功能,需要依赖putenv()函数。下面两种方式均可。

更改php.ini文件方法

找到php.ini文件,搜索putenv关键字,删除即可。

  1. 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,然后设置->禁用函数。

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

删除putenv,然后重启php即可。

实现函数

在count.php同目录下创建文件:count,temp,online,新建文本文档count.txt,去掉扩展名即为count了;

linux系统中请设置文件属性为:777。

文件count.php代码,用到了php函数--explode、isset、empty及sprintf等:

  1. <?php 
  2. $file       = "count"; // 记数文件名称 
  3. $startno     = "1000";  // 起始数值 
  4. $tempfile     = "temp";  
  5. $t_now  = time(); 
  6. $t_array = getdate($t_now); 
  7. $day   = $t_array['mday']; 
  8. $mon   = $t_array['mon']; 
  9. $year  = $t_array['year']; 
  10. if (file_exists("$file")) { 
  11.     $count_info=file("$file"); 
  12.     $c_info = explode(",", $count_info[0]); 
  13.     $total_c=$c_info[0]; 
  14.     $yesterday_c=$c_info[1]; 
  15.     $today_c=$c_info[2]; 
  16.     $lastday=$c_info[3]; 
  17. } else { 
  18.     $total_c="$startno"; 
  19.     $yesterday_c="0"; 
  20.     $today_c="0"; 
  21.     $lastday="0"; 
  22. } 
  23.    
  24. if ( !isset($HTTP_COOKIE_VARS["countcookie"]) || $HTTP_COOKIE_VARS["countcookie"] != $day) { 
  25.     $your_c=1; 
  26.     $lockfile=fopen("temp","a"); 
  27.     flock($lockfile,3); 
  28.     putenv('TZ=JST-9'); 
  29.    
  30.     $t_array2 = getdate($t_now-24*3600); 
  31.     $day2=$t_array2['mday']; 
  32.     $mon2=$t_array2['mon']; 
  33.     $year2=$t_array2['year']; 
  34.     $today = "$year-$mon-$day"; 
  35.     $yesterday = "$year2-$mon2-$day2"; 
  36.     if ($today != $lastday) { 
  37.     
  38.            if ($yesterday != $lastday) $yesterday_c = "0"; 
  39.                else $yesterday_c = $today_c; 
  40.     
  41.         $today_c = 0; 
  42.         $lastday = $today; 
  43.     } 
  44.     $total_c++; 
  45.     $today_c++; 
  46.     $total_c   = sprintf("%06d", $total_c); 
  47.     $today_c   = sprintf("%06d", $today_c); 
  48.     $yesterday_c = sprintf("%06d", $yesterday_c); 
  49.     setcookie("countcookie","$day",$t_now+43200); 
  50.     $fp=fopen("$file","w"); 
  51.     fputs($fp, "$total_c,$yesterday_c,$today_c,$lastday"); 
  52.     fclose($fp); 
  53.     fclose($lockfile); 
  54. } 
  55. if ( emptyempty( $your_c ) ) $your_c = 1; 
  56. setcookie("yourcount",$your_c+1,$t_now+43200); 
  57. $your_c = sprintf("%06d", $your_c); 
  58. //////////////////////////开始统计在线 
  59. $filename="online"; 
  60. $onlinetime=600; //同一IP在线时间,单位:秒 
  61. $online_id=file($filename); 
  62. $total_online=count($online_id); 
  63. $ip=getenv("REMOTE_ADDR"); 
  64. $nowtime=time(); 
  65.  for($i=0;$i<$total_online;$i++){ 
  66.      $oldip=explode("||",$online_id[$i]); 
  67.      $hasonlinetime=$nowtime-$oldip[0]; 
  68.  if($hasonlinetime<$onlinetime and $ip!=$oldip[1]) $nowonline[]=$online_id[$i]; 
  69.                  } 
  70.      $nowonline[]=$nowtime."||".$ip."||"; 
  71.      $total_online=count($nowonline); 
  72.      $fp=fopen($filename,"w"); 
  73.      rewind($fp); 
  74.      for($i=0;$i<$total_online;$i++){ 
  75.      fputs($fp,$nowonline[$i]); 
  76.      fputs($fp,"n"); 
  77.                  } 
  78.  fclose($fp); 
  79.    if($total_online==0)$total_online=1; 
  80.         $total_online = sprintf("%06d", $total_online); 
  81. /////////////////////////////////////////////////////// 
  82. echo "document.write("·总IP访问:".$total_c."");"; 
  83. echo "document.write("<br>");"; 
  84. echo "document.write("·昨日访问:".$yesterday_c."");"; 
  85. echo "document.write("<br>");"; 
  86. echo "document.write("今日IP:".$today_c."");"; 
  87. echo "document.write("&nbsp;");"; 
  88. echo "document.write("·您 访 问:".$your_c."");"; 
  89. echo "document.write("<br>");"; 
  90. echo "document.write("当前在线:".$total_online."");"; 
  91. exit; 
  92. ?> 

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

调用

用JS调用文件count.php

在需要加入统计的的地方,添加:

<script src="/php/count.php"></script>

Tags: php统计IP数 php在线人数

分享到: