在使用iis的时候需要分析日志信息,或蜘蛛抓取日志信息。
如下代码可以方便实现需要的功能。
将如下代码保存到iislog.php里面 访问运行即可:
<?php
/**
* 爱资料php日志分析工具v10
* by jack(chenran)
* time 2016-2-15
* http://www.apizl.com 发布地址:http://www.apizl.com/archives/view-69507-1.html
*/
error_reporting(0);
header("Content-type: text/html; charset=utf-8");
$path = "D:\web\me-web\log\W3SVC28\\";//日志地址
$file_list = scandir($path);
$file_list = array_reverse($file_list);
$count = count($file_list);
$log_name = isset($_GET["log"]) ? $_GET["log"] : "";
if (empty($log_name)) {
$path_ = $path . $file_list[0];
} else {
$path_ = $path . $log_name;
}
$log = file_get_contents($path_);
/**
* 得到每行详细详细
* @param type $log
* @param type $spider
* @return type
*/
function httpinfo_w3c($log, $spider) {
$arrlist = array();
preg_match_all('/(20' . date("y") . '.*' . $spider . '.*)/', $log, $matches); //
for ($i = 0; $i < count($matches[0]); $i++) {
$arr = explode(" ", $matches[0][$i]);
$arr[4] = "http://" . $_SERVER["HTTP_HOST"] . $arr[4];
$arrlist[$i] = $arr;
}
return $arrlist;
}
/**
* 返回总数
* @param type $log
* @param type $spider
* @return type
*/
function spider_count($log, $spider) {
preg_match_all('/' . $spider . '/', $log, $matches); //谷歌 次数
return count($matches[0]);
}
/**
* 向界面输出数据
* @param type $log
* @param type $spider
* @return type
*/
function send_tr($log, $spider) {
$html = "";
$arr = httpinfo_w3c($log, $spider);
for ($i = 0; $i < count($arr); $i++) {
$html.=" <tr>
<td>{$arr[$i][0]} {$arr[$i][1]}</td>
<td>{$arr[$i][3]}</td>
<td>{$arr[$i][4]}</td>
<td>{$arr[$i][8]}</td>
<td>{$arr[$i][9]}</td>
<td>{$arr[$i][10]}</td>
</tr>";
}
return $html;
}
/**
* 输出选择log列表
* @global type $file_list
* @return string
*/
function send_select() {
global $file_list;
$break = 30;
$html = "";
$q = 0;
for ($i = 0; $i < count($file_list); $i++) {
$html .= "<option value=\"$file_list[$i]\">{$file_list[$i]} - " . Send_size($file_list[$i]) . "</option>";
if ($i > $break) {
break;
}
}
return $html;
}
/**
* 输出文件大小
* @global string $path
* @param type $log_name
* @return type
*/
function Send_size($log_name) {
global $path;
$size = (int) filesize($path . $log_name);
$size = format_bytes($size);
return $size;
}
function format_bytes($size) {
$units = array(' B', ' KB', ' MB', ' GB', ' TB');
for ($i = 0; $size >= 1024 && $i < 4; $i++)
$size /= 1024;
return round($size, 2) . $units[$i];
}
?>
<html>
<head>
<title>爱资料php日志分析工具v10</title>
</head>
<body>
<a href=""><h3>爱资料php日志分析工具v10</h3></a>
选择日志:<select onchange=" GetLog(this)">
<?php echo send_select(); ?>
</select>
<a href="#baidu"><h3>百度 - <?php echo spider_count($log, "Baiduspider"); ?>次</h3></a>
<a href="#sm"><h3>神马 - <?php echo spider_count($log, "YisouSpider"); ?>次</h3></a>
<a href="#google"><h3>谷歌 - <?php echo spider_count($log, "Googlebot"); ?>次</h3></a>
<a href="#360"><h3>360 - <?php echo spider_count($log, "360Spider"); ?>次</h3></a>
<h4>------------------------------------------------------------------------</h4>
<h2 onclick="show('baidu');">百度 - <?php echo spider_count($log, "Baiduspider"); ?>次</h2>
<table border=1 id="baidu" style="display: none;">
<?php echo send_tr($log, "Baiduspider"); ?>
</table>
<h2 onclick="show('sm');">神马 - <?php echo spider_count($log, "YisouSpider"); ?>次</h2>
<table border=1 id="sm" style="display: none;">
<?php echo send_tr($log, "YisouSpider"); ?>
</table>
<h2 onclick="show('google');">谷歌 - <?php echo spider_count($log, "Googlebot"); ?>次</h2>
<table border=1 id="google" style="display: none;">
<?php echo send_tr($log, "Googlebot"); ?>
</table>
<h2 onclick="show('360');">360 - <?php echo spider_count($log, "360Spider"); ?>次</h2>
<table border=1 id="360" style="display: none;">
<?php echo send_tr($log, "360Spider"); ?>
</table>
<script type="text/javascript" src="http://www.apizl.com/js/jquery1.4.js"></script>
<script>
$(function() {
$("select").val("<?php echo $log_name; ?>");
})
function show(id)
{
var status = $("#" + id).css("display");
if (status == "none")
{
$("#" + id).css("display", "block");
} else {
$("#" + id).css("display", "none");
}
}
function GetLog(t)
{
var name = $(t).val();
window.location = '?log=' + name;
}
</script>
</body>
</html>
关键字词:

