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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.