pratice in php –简单apache日志分析

September 17, 2007 – 10:25 pm

apache log
今天用php简单写了个apache 日志分析的东西,功能很简单,就是从apache log文件分析获得访问量最大的文件…代码如下:

  1. <?php
  2. /*****从apache log获取访问量最大的文件...***/
  3. $lines = file("access.log");
  4. $log = array(
  5.         "ip" => array() ,
  6.         "file" => array(),
  7.         "time" => array(),
  8.         "method" => array(),
  9.         "status" => array()
  10. );
  11. foreach ($lines as $num => $line) {
  12.     $file_startstrpos($line,' /');
  13.     $file_end = strpos($line,' HTTP');
  14.     $file_length = $file_end - $file_start;
  15.     $file = substr($line,$file_start,$file_length);
  16.     $log['file'][] = $file;
  17.    
  18. }
  19. $file_array =array_count_values($log['file']);
  20. $visited_number = 0;
  21. $most_file = '';
  22. foreach ($file_array as $key => $num) {
  23.     if ($num > $visited_number) {
  24.         $visited_number = $num;
  25.         $most_file = $key;
  26.     }
  27. }
  28. echo $most_file . "<br />";
  29. echo $visited_number;
  30. ?>

Post a Comment

This is a captcha-picture. It is used to prevent mass-access by robots. (see: www.captcha.net)

为了防止垃圾评论,请输入下面的验证码,是 5 个数字加字母组合的验证码。(注:数字 从0到9 ,字母从A到F。)

  

如果看不清验证码,请点击这里