php在线人数据统计程序源码

这种一款可以统计你网站当前在线人数的代码,由php+txt实现的无需数据来来支持,PHP+txt实例代码如下:

  1. <?php
  2. $time = gettimeofday(void);
  3. //文件初始化 start
  4. if(@filesize("time.text")<=0){
  5. $fd_time = fopen("time.text","w+");
  6. fputs($fd_time,$time[sec]);
  7. fclose($fd_time);
  8. $fd_time = fopen("ip.text","w+");
  9. fclose($fd_time);
  10. }
  11. //文件初始化 over
  12. //更新时间 start
  13. $tamp = file("time.text");
  14. $equal = ($time[sec] - $tamp[0]);
  15. if($equal > 60){
  16. $fd_time = fopen("time.text","w+");
  17. fputs($fd_time,"");
  18. fclose($fd_time);
  19. }
  20. //更新时间 over
  21. //检查ip start
  22. $fd_ip = fopen("ip.text","a+");
  23. //获取用户ip地址
  24. if ($http_server_vars["http_x_forwarded_for"])
  25. {
  26. $ip = $http_server_vars["http_x_forwarded_for"];
  27. }
  28. elseif ($http_server_vars["http_client_ip"])
  29. {
  30. $ip = $http_server_vars["http_client_ip"];
  31. }
  32. elseif ($http_server_vars["remote_addr"])
  33. {
  34. $ip = $http_server_vars["remote_addr"];
  35. }
  36. elseif (getenv("http_x_forwarded_for"))
  37. {
  38. $ip = getenv("http_x_forwarded_for");
  39. }
  40. elseif (getenv("http_client_ip"))
  41. {
  42. $ip = getenv("http_client_ip");
  43. }
  44. elseif (getenv("remote_addr"))
  45. {
  46. $ip = getenv("remote_addr");
  47. }
  48. else
  49. {
  50. $ip = "unknown";
  51. }
  52. $ip_adds = file("ip.text");
  53. for($i;$i<count($ip_adds);$i++){
  54. if($ip." "==$ip_adds[$i]){
  55. $ip_check = 1;
  56. break;
  57. }
  58. }
  59. if($ip_check!=1){
  60. fputs($fd_ip,$ip." ");
  61. }
  62. fclose($fd_ip);
  63. //检查ip over
  64. $ip_adds = count(file("ip.text"));
  65. ?>

调用方法:

  1. <?php
  2. /*
  3. * 标题:简单在线人数统计
  4. *
  5. * 作者:邓东东
  6. *开源代码phpfensi.com
  7. **/
  8. ?>
  9. <?php
  10. include("online.php");
  11. $fd_ip = file("ip.text");
  12. ?>
  13. <title>在线人数</title>
  14. <center><font color=blue size=6>php100论坛在线总人数为:<font color=red size=8><?=$ip_adds?>08610071</font>人<br>
  15. 用户ip:<?
  16. echo "<select>";
  17. for($i = 0;$i < count($ip_adds);$i++){
  18. echo"<option value = '$i'>".substr($fd_ip[$i],0,0-strlen(strrchr($fd_ip[$i],'.'))).".*</option> ";
  19. }
  20. echo "</select>";
  21. ?></font></center>