⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 analyse.php

📁 分析APACHE访问日志。并按IP或者访问的页面进行统计。
💻 PHP
字号:
<?php/*	Apache analyse	Power by:antsnet	Email:antsnet@163.com	Date:2007-06-29*//*	get logs*/header("Content-type:text/html;charset=gb2312");$logurl=($_GET["url"]=="" ? "log/access.log":"log/".$_GET["logurl"]);analyse($logurl);function analyse($file="log/access.log",$myurl='',$type='2'){	/*		正则	*/	$basename=basename($file);	$pagesize="18";	$exp_ip="/^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}/";//匹配IP	$exp_date="/\[(.*)]/";//匹配日期	$exp_url="/\"(.*)\"/";//匹配链接	$exp_method_get="/^GET/";//匹配GET方法	$exp_method_post="/^POST/";//匹配POST方法		/*		分析数据并存为数组	*/	$result=getlog($file);//获取日志文件	if($result){	$i=0;	foreach ($result as $rs){				//preg_match($exp_ip,$rs,$single_ip);		preg_match($exp_date,$rs,$single_date);		preg_match($exp_url,$rs,$single_url);		preg_match($exp_method_get,$single_url[1],$single_method_get);		preg_match($exp_method_post,$single_url[1],$single_method_post);				//$ip[$i]=$single_ip[0];//获取访问IP		$date[$i]=str_replace(" +0800","",$single_date[1]);//获取访问时间				$url[$i]=$single_url[1];//获取请求方式网址和协议类型		/*			省略以下代码[delete]		*/		/*		if($single_method_get[0]!=""){		$method_get[$i]=$single_method_get[0];//GET访问方式的行号		}		if($single_method_post[0]!=""){		$method_post[$i]=$single_method_post[0];//获取POST访问的行号		}		*/		/*			省略以上代码[delete]		*/		$http_array=explode(" ",$single_url[1]);				$http_method[$i]=$http_array[0];//访问方式		$http[$i]=$http_array[1];//获取网址		$http_protocol=$http_array[2];//获取访问协议		$i++;				$status_array=explode(" ",$rs);		$http_status[$i]=$status_array[8];//连接的状态		$ip[$i]=$status_array[0];	}	unset($result);//销毁原数组,降低内存使用率		/*		统计相同IP、url的次数	*/	$STRING=<<<EOF<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>Apache日志统计分析</title><style>body{font-size:12px;}ul{list-style:none;border:#ED945A 1px solid;width:150px;height:80px;float:left;padding:5px;line-height:20px;overflow:hidden;onmouseover: expression(onmouseover=function (){this.style.backgroundColor ='#ED945A'}); onmouseout:	expression(onmouseout=function (){this.style.backgroundColor ='white'});}li{cursor:pointer;}.foot{padding-left:90px;padding-top:5px;clear:both;}</style></head><body>EOF;	$STRING.= "<div>";	$ip_amount=array_count_values($ip);	$ip_total=count($ip_amount);	$url_amount=array_count_values($http);		/*		显示数据	*/	$page=($_GET["page"]==""? "1":($_GET["page"]==0 ?"1":$_GET["page"]));	/*		重组URL数组数组	*/	foreach ($url_amount as $key=>$value){		$keys[]=$key;		$values[]=$value;	}	unset($url_amount);	foreach ($ip_amount as $ip_key=>$ip_value){		$ip_keys[]=$ip_key;		$ip_values[]=$ip_value;	}		/*		统计页面访问	*/	if($type==1){	$pages=page($url_amount,$pagesize,$page,'analyze',$basename);	$i=0;	for ($i=$pages["start"];$i<$pages["start"]+$pagesize;$i++){		/*			查找$key 在数组中出现的位置		*/		$url_position=array_keys($http,$keys[$i]);		if($keys[$i]!=""){		$STRING.= "<ul>".$keys[$i]."<font color=\"blue\">访问次数<font color=\"red\">".$values[$i]."</font>次</font>\n";		foreach ($url_position as $line){			$STRING.= "<li>IP:{$ip[$line]}\tDate:{$date[$line]}</li>\n";		}				$STRING.=  "</ul>\n";		}	}	}elseif($type==2){//统计访问信息	$pages=page($ip_amount,$pagesize,$page,'analyze',$basename);	$i=0;	for ($i=$pages["start"];$i<$pages["start"]+$pagesize;$i++){		/*			查找$key 在数组中出现的位置		*/		$ip_position=array_keys($ip,$ip_keys[$i]);		if($ip_keys[$i]!=""){		$STRING.= "<ul>".$ip_keys[$i]."<font color=\"blue\">访问次数<font color=\"red\">".$ip_values[$i]."</font>次</font>\n";		foreach ($ip_position as $line){			$STRING.= "<li>URL:{$http[$line]}\tDate:{$date[$line]}</li>\n";		}				$STRING.=  "</ul>\n";		}	}	unset($ip_amount);	}		$STRING.= "</div>\n";	$STRING.="<div class=\"foot\">{$pages["page"]}</div>";	$STRING.= "<div class=\"foot\">总访问量:{$pages["amount"]} 访问IP:{$ip_total}</div>";	$STRING.=<<<EOF</body></html>EOF;}	echo $STRING;}function getlog($file){	$myArray=array();	$contents=@file($file);	if(!$contents){	return false;	}else{	foreach ($contents as $con){		$con=preg_replace("[\r\n|\n|\r]","",$con);		if($con!=""){		array_push($myArray,$con);		}	}	return $myArray;	}	}/*	将日志分页处理*/function page($array,$pagesize,$current,$url,$log){/*calculator*/$amount=Count($array);$total=ceil(Count($array)/$pagesize);$prev=(($current-1)<=0 ? "1":($current-1));$next=(($current+1)>=$total ?   $total:$current+1);$current=($current>($total)?($total):$current);$pagearray["start"]=(($current-1<=0 ?"1":$current)-1)*$pagesize;$firstpav=($current<=1?"第一页":"<a href=\"#{$url}\" onclick=\"analyze('{$log}','0');\">第一页</a>");$prevpav=($total-1<=0 ?"上一页":($current==1? "上一页":"<a href=\"#{$url}\"  onclick=\"analyze('{$log}','$prev');\">上一页</a>"));$nextpav=($current+1<=$total?($total-1>0 ? "<a href=\"#{$url}\" onclick=\"analyze('{$log}','$next');\">下一页</a>":"下一页"):"下一页");$endpage=($current==$total? "最后一页":($total==1?"最后一页":"<a href=\"#\" onclick=\"analyze('{$log}','$total');\">最后一页</a>"));$pagearray["page"]="共<font color=\"red\">{$amount}</font>条记录<font color=\"red\">{$current}</font>/{$total}页 {$firstpav} {$prevpav} {$nextpav} {$endpage}";$pagearray["amount"]=$amount;return $pagearray;}?>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -