Анализиране на ред от файл

d_badboy

Registered
Здравейте, става въпрос за анализиране на лог файл на Apache.
Един ред от него изглежда така:
127.0.0.1 - - [24/Apr/2009:12:25:35 +0300] "GET /index.php?link=home HTTP/1.1" 200 7332
Понеже не съм много наясно с регулярните изрази искам да взема тези части оцветени в червено :)
 
Като гледам аз моя лог файл, това би трябвало да е кое IP кога и къде е влизало. В твоя случай от IP 127.0.0.1 на 24/Apr/2009:12:25:35 +0300 е влизано в тоя линк index.php?link=home
 
Пробвай:
<?php
$string = '127.0.0.1 - - [24/Apr/2009:12:25:35 +0300] "GET /index.php?link=home HTTP/1.1" 200 7332';

preg_match("#([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+).*?\[([0-9]+/[a-zA-Z]+/[0-9]+:[0-9]+:[0-9]+[0-9]+).*?\]\s\"GET\s(.*?)\s.*?#", $string, $matches);
echo $matches[1]."<br />".$matches[2]."<br />".$matches[3];
?>
 
имах един скрипт за анализиране на логове:

apache-log-parser.php
Код:
<?php
/*
+----------------------------------------------+
|                                              |
|         PHP apache log parser class          |
|                                              |
+----------------------------------------------+
| Filename   : apache-log-parser.php           |
| Created    : 21-Sep-05 23:28 GMT             |
| Created By : Sam Clarke                      |
| Email      : admin@free-webmaster-help.com   |
| Version    : 1.0                             |
|                                              |
+----------------------------------------------+


LICENSE

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License (GPL)
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

To read the license please visit http://www.gnu.org/copyleft/gpl.html

*/

class apache_log_parser
{

  var $bad_rows; // Number of bad rows
  var $fp; // File pointer

  function format_log_line($line)
  {
    preg_match("/^(\S+) (\S+) (\S+) \[([^:]+):(\d+:\d+:\d+) ([^\]]+)\] \"(\S+) (.*?) (\S+)\" (\S+) (\S+) (\".*?\") (\".*?\")$/", $line, $matches); // pattern to format the line
    return $matches;
  }

  function format_line($line)
  {
    $logs = $this->format_log_line($line); // format the line

    if (isset($logs[0])) // check that it formated OK
    {
      $formated_log = array(); // make an array to store the lin info in
      $formated_log['ip'] = $logs[1];
      $formated_log['identity'] = $logs[2];
      $formated_log['user'] = $logs[2];
      $formated_log['date'] = $logs[4];
      $formated_log['time'] = $logs[5];
      $formated_log['timezone'] = $logs[6];
      $formated_log['method'] = $logs[7];
      $formated_log['path'] = $logs[8];
      $formated_log['protocal'] = $logs[9];
      $formated_log['status'] = $logs[10];
      $formated_log['bytes'] = $logs[11];
      $formated_log['referer'] = $logs[12];
      $formated_log['agent'] = $logs[13];
      return $formated_log; // return the array of info
    }
    else
    {
      $this->badRows++; // if the row is not in the right format add it to the bad rows
      return false;
    }
  }

  function open_log_file($file_name)
  {
    $this->fp = fopen($file_name, 'r'); // open the file
    if (!$this->fp)
    {
      return false; // return false on fail
    }
    return true; // return true on sucsess
  }

  function close_log_file()
  {
    return fclose($this->fp); // close the file
  }

  function get_line($line_length=300)
  {
    return fgets($this->fp, $line_length); // true and get a line and return the result
  }

}
?>
и example.php
Код:
<?php
/*
+----------------------------------------------+
|                                              |
|      PHP example apache log parser class     |
|                                              |
+----------------------------------------------+
| Filename   : example.php                     |
| Created    : 21-Sep-05 23:28 GMT             |
| Created By : Sam Clarke                      |
| Email      : admin@free-webmaster-help.com   |
| Version    : 1.0                             |
|                                              |
+----------------------------------------------+


LICENSE

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License (GPL)
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

To read the license please visit http://www.gnu.org/copyleft/gpl.html

*/

include 'apache-log-parser.php';

$apache_log_parser = new apache_log_parser(); // Create an apache log parser

if ($apache_log_parser->open_log_file('example.log')) // Make sure it opens the log file
{
  while ($line = $apache_log_parser->get_line()) { // while it can get a line
    $parsed_line = $apache_log_parser->format_line($line); // format the line
    print_r($parsed_line); // print out the array
  }
  $apache_log_parser->close_log_file(); // close the log file
}
else
{
  echo 'Sorry cannot open log file.';
}
?>
:)
при мен на линукса работи и го ползвам
 
emagi твоя код работи безпроблемно.
alex95_bg_2 твоя код мисля че е за php4 и трябва да се преработи за php5.
Като цяло кода на emagi ми върши страхотна работа. Мерси +1
 
alex95_bg_2 каза:
моя код работи под PHP 5.2.6 иначе
На мен ми изкара празна страница когато въведох в example.php този път
'C:\...\logs\access.log'
emagi една грешка само има :D
изпуснал си едно двуеточие но не е фатално
и разделих датата от часа
Код:
<?php
$string = '127.0.0.1 - - [24/Apr/2009:12:25:35 +0300] "GET /index.php?link=home HTTP/1.1" 200 7332';

preg_match("#([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+).*?\[([0-9]+/[a-zA-Z]+/[0-9]+):([0-9]+:[0-9]+[color=red]:[/color][0-9]+).*?\]\s"GET\s(.*?)\s.*?#", $string, $matches);
echo $matches[1]."<br />".$matches[2]."<br />".$matches[3]."<br />".$matches[4];
?>
 

Back
Горе