The following program will parse your standard Web server's log file and print the results in a formatted HTML table. An example log file entry might be the following: 212.27.53.112 - - [25/March/2000:05:10:29 +100] "GET /site.htm HTTP/1.0" 200 1892 The program: <?php $fp = @fopen($logfile_location, "r") or die("Cannot Open $logfile_location"); while ($line = fgets($fp, 1024)) { $line = trim($line); preg_match("/^(\S+)\s+(\S+)\s+(\S+)\s+\[(.*)\] \s+\"(.*)\"\s+(\S+)\s+(\S+)$/x", $line, $matches); array_shift($matches); $host = $matches[0]; $identity = $matches[1]; $user = $matches[2]; $time = $matches[3]; $url = $matches[4]; $success = $matches[5]; $bytes = $matches[6]; preg_match("@(..)/(...)/(....):(..):(..):(..)@", $time, $matches); array_shift($matches); $day = $matches[0]; $mon = $matches[1] + 1; $year = $matches[2]; $hour = $matches[3]; $minutes = $matches[4]; $seconds = $matches[5]; preg_match("/\S+\s+(\S+)/", $url, $matches); $url = $matches[1]; if ($success == 200) { $success[$i++] = array($host, $identity, $user, array($day, $mon, $year, $hour, $minutes, $seconds), $url, $bytes); } else { $failure[$x++] = array($host, $identity, $user, array($day, $mon, $year, $hour, $minutes, $seconds), $url, $bytes); } } ?> <html> <head> <title> Here is the results of your access logs </title> </head> <body> <h1> Reports on your Access logs </h1> <h2> Successful Accesses </h2> <br> <table border="1" cellpadding="2" cellspacing="1"> <tr> <th> Host </th> <th> Identity </th> <th> User </th> <th> Time </th> <th> Url </th> <th> Bytes </th> </tr> <?php for ($idx = 0; $idx < count($success); $idx++) { ?> <tr> <td> <?php echo $success[$idx][0]; ?> </td> <td> <?php echo $success[$idx][1]; ?> </td> <td> <?php echo $success[$idx][2]; ?> </td> <td> <?php echo "On: " . $success[$idx][3][1] . "/"; echo $success[$idx][3][0] . "/"; echo $success[$idx][3][2] . ", "; echo "at: " . $success[$idx][3][3] . ":"; echo $success[$idx][3][4] . ":" . $success[$idx][3][5]; ?> </td> <td> <?php echo $success[$idx][4]; ?> </td> <td> <?php echo $success[$idx][5]; ?> </td> </tr> <?php } ?> </table> <br><br> <h2> Failures </h2> <br> <table border="1" cellpadding="2" cellspacing="1"> <tr> <th> Host </th> <th> Identity </th> <th> User </th> <th> Time </th> <th> Url </th> <th> Bytes </th> </tr> <?php for ($idx = 0; $idx < count($failure); $idx++) { ?> <tr> <td> <?php echo $failure[$idx][0]; ?> </td> <td> <?php echo $failure[$idx][1]; ?> </td> <td> <?php echo $failure[$idx][2]; ?> </td> <td> <?php echo "On: " . $failure[$idx][3][1] . "/"; echo $failure[$idx][3][0] . "/"; echo $failure[$idx][3][2] . ", "; echo "at: " . $failure[$idx][3][3] . ":"; echo $failure[$idx][3][4] . ":" . $failure[$idx][3][5]; ?> </td> <td> <?php echo $failure[$idx][4]; ?> </td> <td> <?php echo $failure[$idx][5]; ?> </td> </tr> <?php } ?> </table> </body> </html> |