Jump to content

Very simple problem that I cannot figure out for the life of me.. (please help!)


dustywusty

Recommended Posts

Hey guys...going crazy trying to figure this out.

 

I was given a log to parse the top level directory out of...so I did so using multiple explodes and that worked fine. Now I need to count how many times each directory is accessed...I cannot for the life of me figure out how to either use another array to keep track of the number of hits or modify the existing array to include another value for counting each hit. My instructor told me that it could be done in a line of code...  Please any pointers or anything would be appreciated!

 

<?php

 

$log = file('./access.short', FILE_USE_INCLUDE_PATH);

$rootDir = 1;

 

 

foreach ($log as $line)

  {

  $rootDir = 0;

 

    $logArray = explode('"GET ',$line);

    //echo $logArray[1];

    //echo "<br>";

    $logArrayTwo = explode('HTTP/1.',$logArray[1]);

    //echo $logArrayTwo[0];

    //echo "<br>";

    $logArrayThree = explode('/',$logArrayTwo[0]);

 

//$logArrayThree = array ((explode('/',$logArrayTwo[0])), 'count' => 1);

 

//$myvariable=array('color'=>'red','size'=>'big','price'=>16);

 

 

//$logArrayThree[1] = "+1;

 

 

 

$period = ".";

//if (strstr($logArray[1],$period))

//  {

//    $logArrayThree[1] = 'ROOT';

//  }

 

      if (trim($logArrayThree[1]) == "")

      {

        $rootDir = $rootDir +1;

      $logArrayThree[1] = "ROOT DIR ($rootDir)";

      }

    //print_r($logArrayThree[1]);

 

 

//if ($logArrayThree[1] == $count[1])

//  {

//      $count[1] = $count[1] + 1;

//      echo ($logArrayThree[1] + $count[1]);

//  }

 

 

//$count = array($logArrayThree[1]=>($count[1] +1));

//print_r($count);

//print_r($logArrayThree[1]);

//echo $count;

echo "<br>";

 

//$logArrayThree[1] = $logArrayThree[1 +1];

//print_r(array_count_values($logArrayThree));

 

    echo ($logArrayThree[1]);

  //echo "<br>";

    //print_r($logArrayThree[1]);

  // print($logArrayThree);

  }

 

 

?>

 

and a bit of the log

 

66.249.71.203 - - [07/Oct/2008:23:32:33 -0400] "GET /read/50448 HTTP/1.1" 200 7896

75.68.79.202 - - [07/Oct/2008:23:32:33 -0400] "GET /admit/pdfs/Application.pdf HTTP/1.1" 200 214516

66.249.71.91 - - [07/Oct/2008:23:32:33 -0400] "GET /mtd/faculty/staff/music/NEBDI/festivals.html HTTP/1.1" 200 4807

67.195.37.110 - - [07/Oct/2008:23:32:33 -0400] "GET /gallery/2006-07/agames.html HTTP/1.0" 200 2696

158.136.1.159 - - [07/Oct/2008:23:32:34 -0400] "GET /webapp/portal/channel/email/ HTTP/1.1" 200 215

66.249.71.230 - - [07/Oct/2008:23:32:32 -0400] "GET /read/197350 HTTP/1.1" 200 9988

76.178.196.159 - - [07/Oct/2008:23:32:32 -0400] "GET /webcams/image1.jpg HTTP/1.1" 200 59360

 

I've gotten it down to parsing out the path only. give this a go, and if you need help from here, post the issue here:

<?php
$data = "6.249.71.203 - - [07/Oct/2008:23:32:33 -0400] \"GET /read/50448 HTTP/1.1\" 200 7896
75.68.79.202 - - [07/Oct/2008:23:32:33 -0400] \"GET /admit/pdfs/Application.pdf HTTP/1.1\" 200 214516
66.249.71.91 - - [07/Oct/2008:23:32:33 -0400] \"GET /mtd/faculty/staff/music/NEBDI/festivals.html HTTP/1.1\" 200 4807
67.195.37.110 - - [07/Oct/2008:23:32:33 -0400] \"GET /gallery/2006-07/agames.html HTTP/1.0\" 200 2696
158.136.1.159 - - [07/Oct/2008:23:32:34 -0400] \"GET /webapp/portal/channel/email/ HTTP/1.1\" 200 215
66.249.71.230 - - [07/Oct/2008:23:32:32 -0400] \"GET /read/197350 HTTP/1.1\" 200 9988
76.178.196.159 - - [07/Oct/2008:23:32:32 -0400] \"GET /webcams/image1.jpg HTTP/1.1\" 200 59360";
$data_array = explode("\n",$data);
foreach ($data_array as $val){
$tmp_a = explode("GET ", $val);
$tmp_b = explode("HTTP/", $tmp_a[1]);
print $tmp_b[0]."\n";
}

P.S. I know that there's an easier way to do this, but I tend to code the quickest way first.

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.