pratice in php –简单apache日志分析
September 17, 2007 – 10:25 pm
今天用php简单写了个apache 日志分析的东西,功能很简单,就是从apache log文件分析获得访问量最大的文件…代码如下:
- <?php
- /*****从apache log获取访问量最大的文件...***/
- $lines = file("access.log");
- $log = array(
- "ip" => array() ,
- "file" => array(),
- "time" => array(),
- "method" => array(),
- "status" => array()
- );
- foreach ($lines as $num => $line) {
- $file_start = strpos($line,' /');
- $file_end = strpos($line,' HTTP');
- $file_length = $file_end - $file_start;
- $file = substr($line,$file_start,$file_length);
- $log['file'][] = $file;
- }
- $file_array =array_count_values($log['file']);
- $visited_number = 0;
- $most_file = '';
- foreach ($file_array as $key => $num) {
- if ($num > $visited_number) {
- $visited_number = $num;
- $most_file = $key;
- }
- }
- echo $most_file . "<br />";
- echo $visited_number;
- ?>