使用PHP来统计APACHE访问日志
时间:2006-12-21 15:06:54
来源: 作者:whsong 点击:次 出处:技术无忧
关键字:使用
<?php /*
程序:APACHE 访问日志统计
*/
class myapachelog{
var $filename; //日志文件名 完全路径
function myapachelog($filename)
{
$this->filename=$filename;
}
//该函数为格式转换函数,将类似的:$str = "03/Mar/2005:16:53:32"转化为相应的UNIX时间戳。
function Format2UnixTime ($str)
{
$time = $str;
$time = str_replace("/"," ",$time);
$time_array = explode( ":",$time,2);
$time = $time_array[0]." ".$time_array[1];
return strtotime($time);
}
function CompareByTimes ($x,$y)//帮助方法
{
if ( $x[0] == $y[0] )
return 0;
else if ($x[0] > $y[0])
return -1;
else
return 1;
}
function CompareByAccessTime ($x,$y)//帮助方法
{
$x[1] = Format2UnixTime($x[1]); //先格式化为UNIX时间戳
$y[1] = Format2UnixTime($y[1]); //先格式化为UNIX时间戳
if ( $x[1] == $y[1] )
return 0;
else if ($x[1] > $y[1])
return -1;
else
return 1;
}
function mytablestyle(){
$style=<<<STYLE
<style type='text/css'>
.table1 {border-top: #cae0ed solid 1px;}
.table1 td {
height: 16px;
font-size: 16px;
border-bottom: #cae0ed solid 1px;
background-color: expression((this.parentElement.sectionRowIndex%2==0)?'#fff':'#f2f8fb')
}
</style>
STYLE;
echo $style;
}
/*$order为排序方式: 0:按访问次数排序(缺省值) 1:按最近一次访问的时间排序*/
function GetAccessByLog ($timeformat=1,$order=0)
{
$this->mytablestyle();
if ( file_exists($this->filename) )
{
$handle = fopen ($this->filename, "r");
$ip_times = array();
while (!feof ($handle)) {//读入日志文件内容
$buffer = fgets($handle, 999);
if ((preg_match("#/d{1,2}///w{1,3}///d{1,4}/:/d{1,2}/:/d{1,2}/:/d{1,2}#",$buffer,$access_time)) && (preg_match("#/d{1,3}/./d{1,3}/./d{1,3}/./d{1,3}#",$buffer,$ip)))
{
$ip = $ip[0];
$access_time = $access_time[0];
$access_time=$this->Format2UnixTime($access_time);
$access_time=strftime(" %Y-%m-%d %H:%M:%S " ,$access_time);
if ( in_array($ip,array_keys($ip_times)))
{
$ip_times[$ip][0]++;//$ip_times[$ip][0]为访问次数times
$ip_times[$ip][1]=$access_time;//$ip_times[$ip][1]为访问时间access_time
}else
{
$ip_times[$ip][0] = 1;
$ip_times[$ip][1]=$access_time;
}
}
}
fclose ($handle);
}else
{
echo $this->filename;
echo "<font color=red>日志文件不存在,请检查路径</font>";
exit;
}
if ( $order==1 )
{
$compare = "CompareByAccessTime";//按最近一次访问时间排序方法
$title = "<b>按最近一次访问时间排序</b>";
}else{
$compare = "CompareByTimes"; //按访问次数排序方法
$title = "<b>按访问次数排序</b>";
}
//sort($ip);
//uasort( $ip_times, $compare );
echo $title;
echo "<table class=table1><tr><td>IP</td><td><b><font color=green>访问次数</font></b></td><td>最近一次访问时间</td></tr>";
foreach ( $ip_times as $ip=>$value )
{
echo "<tr><td>".$ip."</TD><TD><b><font color=green>".$value[0]."</font></b></TD><TD>".$value[1]."</TD></TR>";
}
echo "</table>";
}
//查看部分日志记录 //从start到end去件的记录
function GetAccessByLogPart ($start,$end) //$start开始 $end结束
{
$this->mytablestyle();
if ( file_exists($this->filename) )
{
$handle = fopen ($this->filename, "r");
$ip_times = array();
$i=0;
echo $start."---".$end;
echo "<table class=table1><tr><td>编号</td><td>访问时间</td><td>来访者IP</td></tr>";
while (!feof ($handle)) {//读入日志文件内容
$i++;
$buffer = fgets($handle, 999);
if($i>$start&&$i<$end) {
echo "<tr>";
if ((preg_match("#/d{1,2}///w{1,3}///d{1,4}/:/d{1,2}/:/d{1,2}/:/d{1,2}#",$buffer,$access_time)) && (preg_match("#/d{1,3}/./d{1,3}/./d{1,3}/./d{1,3}#",$buffer,$ip)))
{
$ip = $ip[0];
$access_time = $access_time[0];
$access_time=$this->Format2UnixTime($access_time);
$access_time=strftime(" %Y-%m-%d %H:%M:%S " ,$access_time);
echo "<td>".($i)."</td>";//$ip_times[$ip][0]为访问次数times
echo "<td>".$ip_times[$ip][1]=$access_time;//$ip_times[$ip][1]为访问时间access_time
echo "</td><td>$ip</td>";
}
echo "</tr>";
}
}
echo "</table>";
fclose ($handle);
}else
{
echo $this->filename;
echo "<font color=red>日志文件不存在,请检查路径</font>";
exit;
}
}
//从$start时间到$end时间的记录
function GetAccessByLogPart_Bytime ($start,$end) //根据访问时间
{
$this->mytablestyle();
if ( file_exists($this->filename) )
{
$handle = fopen ($this->filename, "r");
$ip_times = array();
$i=0;
echo $start."---".$end;
echo "<table class=table1><tr><td>编号</td><td>访问时间</td><td>来访者IP</td></tr>";
while (!feof ($handle)) {//读入日志文件内容
$i++;
$buffer = fgets($handle, 999);
if($i>$start&&$i<$end) {
if ((preg_match("#/d{1,2}///w{1,3}///d{1,4}/:/d{1,2}/:/d{1,2}/:/d{1,2}#",$buffer,$access_time)) && (preg_match("#/d{1,3}/./d{1,3}/./d{1,3}/./d{1,3}#",$buffer,$ip)))
{
$ip = $ip[0];
$access_time = $access_time[0];
$access_time=$this->Format2UnixTime($access_time);
$access_time=strftime(" %Y-%m-%d %H:%M:%S " ,$access_time);
echo "<tr><td>".$ip_times[$ip][0]++."</td>";//$ip_times[$ip][0]为访问次数times
echo "<td>".$ip_times[$ip][1]=$access_time;//$ip_times[$ip][1]为访问时间access_time
echo "</td><td>$ip</td></tr>";
}
}
}
echo "</table>";
fclose ($handle);
}else
{
echo $this->filename;
echo "<font color=red>日志文件不存在,请检查路径</font>";
exit;
}
}
}
/**
* **********测试
*/
$my=new myapachelog("C:/Program Files/PHP Home Edition 2/Apache2/logs/access.log");
$my->GetAccessByLog();//0:按访问次数排序(缺省值);1:按最近一次访问的时间排序。
?>
想自己动手组装电脑吗?想了解市场行情吗?来技术无忧DIY资讯一切烦脑都没有!












文章评论
共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面