Posted By

lfatr on 02/16/10


Tagged

apache


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

dantreacy


PHP SCRIPT PARSING APACHE LOG


 / Published in: PHP
 

  1. <?php
  2. ini_set("memory_limit","64M");
  3.  
  4. $log_file = '/var/log/httpd/...';
  5. $pattern = '/GET (\S*news\/images\/[0-9]{4}\S*.php)/';
  6.  
  7. if (is_readable($log_file)) {
  8.  
  9. $fh = fopen($log_file,'r') or die($php_errormsg);
  10. $requests = array();
  11.  
  12. header('Content-Type: text/xml');
  13. while (!feof($fh)) {
  14. if ($s = trim(fgets($fh,16384))) {
  15. if (preg_match($pattern, $s, $matches)) {
  16. list($whole_match, $request) = $matches;
  17. $requests[$request]++;
  18. }
  19. }
  20. }
  21. fclose($fh) or die($php_errormsg);
  22.  
  23. // sort the array (in reverse) by number of requests
  24. arsort($requests);
  25.  
  26. // print formatted results
  27. print "<?xml version='1.0' encoding='UTF-8'?>\n";
  28. print "<images>\n";
  29. foreach ($requests as $request => $accesses) {
  30. printf("\t<image path='%s' count='%s' />\n", $request, $accesses);
  31. }
  32. print "</images>\n";
  33. } else {
  34. echo "cannot access logfile!";
  35. }
  36. ?>

Report this snippet  

You need to login to post a comment.