自己用PHP來分析Apache訪問日誌 ____PHP

來源:互聯網
上載者:User

根據實際工作需要,想從訪問日誌裡找出自己想要的東西,如找不到的檔案,從google來的還是從yahoo來的或從別的地方來的,還是搜尋引擎的蜘蛛訪問。原理很簡單就是開啟檔案,過濾不要的記錄,分解記錄欄位,列表所需結果。幾乎憑一個PHP的函數preg_match()搞定。下面是原始碼,自己研究吧 。
<html>
<head>
<title>
Simple tools for website logs
</title>
</head>
<body>
<form name="my_form" method="post">
  Select your type :<br>
 <select name="type">
  <option value="">Get the null links</option>
  <option value="yahoo">Acess from yahoo</option>
  <option value="google">Access from google</option>
  <option value="msn">Access from Msn</option>
  <option value="robot">Access by robots</option>
 </select>
 &nbsp;
 <input type="submit" name="submit" value="get the result">
</form>
<table border=1>
  <tr bgcolor="#FFCCFF">
    <td><font color="#000000">ClientIP</font></td>
    <td><font color="#000000">AccessTime</font></td>
    <td><font color="#000000">TargetPage</font></td>
    <td><font color="#000000">Code</font></td>
    <td><font color="#000000">FromURL</font></td>
    <td><font color="#000000">Client ENV</font></td>
  </tr>
<?PHP
$doc_path= $_SERVER["DOCUMENT_ROOT"];
if(substr($doc_path,-1)!="/"){
 $doc_path=$doc_path."/";
}

if($type=='yahoo'){
        $lines = file ($doc_path.'logs/access_log');
        foreach ($lines as $line_num => $line) {
                if (preg_match ("/yahoo/i",strtolower($line))) {
                        if (!preg_match ("/slurp/",strtolower($line))){
                                preg_match("/([0-9.]+)?([ -]+)?(/[)?([0-9a-zA-Z+: //]+)?(/])?( /"GET //)?([a-z0-9A-Z.///?&=%_/-:+]+)?( HTTP//1.[1|0|2]/" )?([0-9.]+)?( )?([0-9./-]+)?( /")?([a-z0-9A-Z.///?&=%_/-:+]+)?(/" /")?(.*)/i",$line, $matches);
        echo "<tr><td>".$matches[1]."</td><td>".$matches[4]."</td><td>".$matches[7]."</td><td>".$matches[9]."</td><td>".$matches[13]."</td><td>".$matches[15]."</td><tr>";
                        }
                }

        }
}elseif($type=="robot"){
        $lines = file ($doc_path.'logs/access_log');
        foreach ($lines as $line_num => $line) {
                 if (!preg_match("/robots.txt/i",$line)){      
      if (preg_match ("/(slurp)|(msnbot)|(googlebot)|(psbot)/i",strtolower($line))){
                                preg_match("/([0-9.]+)?([ -]+)?(/[)?([0-9a-zA-Z+: //]+)?(/])?( /"GET //)?([a-z0-9A-Z.///?&=%_/-:+]+)?( HTTP//1.[1|0|2]/" )?([0-9.]+)?( )?([0-9./-]+)?( /")?([a-z0-9A-Z.///?&=%_/-:+]+)?(/" /")?(.*)/i",$line, $matches);
        echo "<tr><td>".$matches[1]."</td><td>".$matches[4]."</td><td>".$matches[7]."</td><td>".$matches[9]."</td><td>".$matches[13]."</td><td>".$matches[15]."</td><tr>";
                        }
      } 
        }
}elseif($type!=""){
        $lines = file ($doc_path.'logs/access_log');
        foreach ($lines as $line_num => $line) {
                if (preg_match ("/$type/i",strtolower($line))) {
                        if (!preg_match ("/".$type."bot/",strtolower($line))){
                            preg_match("/([0-9.]+)?([ -]+)?(/[)?([0-9a-zA-Z+: //]+)?(/])?( /"GET //)?([a-z0-9A-Z.///?&=%_/-:+]+)?( HTTP//1.[1|0|2]/" )?([0-9.]+)?( )?([0-9./-]+)?( /")?([a-z0-9A-Z.///?&=%_/-:+]+)?(/" /")?(.*)/i",$line, $matches);
       echo "<tr><td>".$matches[1]."</td><td>".$matches[4]."</td><td>".$matches[7]."</td><td>".$matches[9]."</td><td>".$matches[13]."</td><td>".$matches[15]."</td><tr>";
                        }
                }

        }

}else{
 $lines = file ($doc_path.'logs/access_log');
 foreach ($lines as $line_num => $line) {
  if (preg_match ("/ 404 /i",$line)) {
   if (!preg_match ("/robots.txt/",$line)){
                            preg_match("/([0-9.]+)?([ -]+)?(/[)?([0-9a-zA-Z+: //]+)?(/])?( /"GET //)?([a-z0-9A-Z.///?&=%_/-:+]+)?( HTTP//1.[1|0|2]/" )?([0-9.]+)?( )?([0-9./-]+)?( /")?([a-z0-9A-Z.///?&=%_/-:+]+)?(/" /")?(.*)/i",$line, $matches);
       echo "<tr><td>".$matches[1]."</td><td>".$matches[4]."</td><td>".$matches[7]."</td><td>".$matches[9]."</td><td>".$matches[13]."</td><td>".$matches[15]."</td><tr>";
   }
  }
 
 }
}
?>
</table>
</body>
</html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.