Jump to content

Parsing an apache logfile


lazaruz

Recommended Posts

I am trying to parse an apache logfile to count how many unique downloads I have of certain files.

I have the lines parsed and read them one by one and and adds the line if it does not already exists in an array.

 

if(stristr($parsed_line['path'], '.rar'))
{
   $element = $parsed_line['ip'] . " " .$parsed_line['date'] . " " . $parsed_line['path'];

   if(stristr($parsed_line['path'], 'somefile1.rar'))
      if(Add($element, $arr1))
      {
         $arr1[count($arr1)] = $element;
      }
   if(stristr($parsed_line['path'], 'somefile2.rar'))
      if(Add($element, $arr2))
      {
         $arr2[count($arr2)] = $element;
      }			
}

 

I test the lines with:

function Add($element, $arr)
{
   foreach($arr as $line)
   {
      if($line == $element)
      return false;
   }
   return true;
}

 

And print the result with:

echo "Somefile1: " . count($arr1);
echo "<br>";
echo "Somefile2: " . count($arr2);

 

Now, this works but there must be a better way to do it.

The way I'm doing it now results in that I have to create one array for each file I'm looking for and add a codeblock to add that specific file.

I'm looking for a way to place all valid lines in one array and count them based on filename. This way I don't have to update the php-code if I add a new file to download.

Link to comment
https://forums.phpfreaks.com/topic/80864-parsing-an-apache-logfile/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.