写在前面的话
很多人有这样的需求,就是统计网站访问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等:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
<?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
在需要加入统计的的地方,添加:
1
|
<script src= "/php/count.php" ></script>
|
到此这篇关于php实现统计ip数及在线人数的示例代码的文章就介绍到这了,更多相关php 统计ip数及在线人数内容请搜索快网idc以前的文章或继续浏览下面的相关文章希望大家以后多多支持快网idc!
原文链接:https://www.wjcms.net/
相关文章
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
2025-05-29 74
-
2025-05-29 30
-
2025-05-27 53
-
2025-05-29 83
-
2025-05-25 17