body { white-space: normal; }'; } else { echo ''; } ////////////////////////////////////////////////////// // 以下よりphpソースコード //$logFile = "/home/ymda/nginx/access.log_2026-09-10"; $logFile = "/home/ymda/nginx/access.log"; date_default_timezone_set('Asia/Tokyo'); if (!file_exists($logFile)) { die("ログファイルが見つかりません\n"); } echo "当日のWebサーバーログファイルを解析します。0時0分0秒頃にリセットされます。以下に表示がなければview-source、もしくはbotでのアクセスはないものとみられます。リアルタイムで反映するものの、反映するのに少し時間がかかることがあります。
"; $fp = fopen($logFile, 'r'); $lastRoot = []; // ログ全体の最後のアクセス時刻 $lastLogTimestamp = 0; while (($line = fgets($fp)) !== false) { $host = ''; // ホスト名を確認 if (!preg_match('/^https?:\/\/([^\/\s]+)/', $line, $m)) { continue; } $host = $m[1]; if ($host !== 'demo4.773.moe') { continue; } // Nginx日時 if (!preg_match( '/\[([0-9]{2}\/[A-Za-z]{3}\/[0-9]{4}:[0-9]{2}:[0-9]{2}:[0-9]{2} [+\-][0-9]{4})\]/', $line, $m )) { continue; } $dt = DateTime::createFromFormat( 'd/M/Y:H:i:s O', $m[1] ); if (!$dt) { continue; } $accessTimestamp = $dt->getTimestamp(); // ログ全体の最後の時刻を記録 if ($accessTimestamp > $lastLogTimestamp) { $lastLogTimestamp = $accessTimestamp; } // IP if (!preg_match( '/^\S+\s+(\d{1,3}(?:\.\d{1,3}){3})\s/', $line, $m )) { continue; } $ip = $m[1]; // Path if (!preg_match( '/"GET\s+(\S+)\s+HTTP/', $line, $m )) { continue; } $requestPath = $m[1]; // ? 以降を除去 $path = parse_url( $requestPath, PHP_URL_PATH ); // 対象は / と /1x1.gif のみ if ( $path !== '/' && $path !== '/1x1.gif' ) { continue; } // User-Agent if (!preg_match( '/"([^"]*)"\s*$/', trim($line), $m )) { continue; } $userAgent = $m[1]; /* * / * のアクセスを記録 */ if ($path === '/') { $lastRoot[$ip] = [ 'timestamp' => $accessTimestamp, 'line' => trim($line), 'date' => $dt->format('Y-m-d H:i:s'), 'ip' => $ip, 'userAgent' => $userAgent, ]; continue; } /* * /1x1.gif * が来た場合 */ if ($path === '/1x1.gif') { if (!isset($lastRoot[$ip])) { continue; } $root = $lastRoot[$ip]; $diff = $accessTimestamp - $root['timestamp']; /* * / → gif が9秒以上 */ if ($diff >= 9) { $results[] = [ 'timestamp' => $root['timestamp'], // / の時刻基準 'html' => "【9秒以上離れたアクセス】 - ソース表示かbotの疑い
\n" . "Host : " . $host . "
\n" . "IP : " . htmlspecialchars($ip) . "
\n" . "間隔 : " . $diff . " 秒

\n" . "Path : /
\n" . "日時 : " . htmlspecialchars($root['date']) . "
\n" . "User-Agent : " . htmlspecialchars($root['userAgent']) . "

\n" . "Path : " . htmlspecialchars($requestPath) . "
\n" . "日時 : " . htmlspecialchars($dt->format('Y-m-d H:i:s')) . "
\n" . "User-Agent : " . htmlspecialchars($userAgent) . "
\n" . "
\n" ]; } // gifが確認できたのでリセット unset($lastRoot[$ip]); } } /* * -------------------------------------------------- * / はあるが、その後gifが来なかったものを調べる * -------------------------------------------------- */ foreach ($lastRoot as $ip => $root) { $host="demo4.773.moe"; /* * ログの最後から9秒以上経過している */ $diff = $lastLogTimestamp - $root['timestamp']; if ($diff < 9) { continue; } $results[] = [ 'timestamp' => $root['timestamp'], 'html' => "【gifアクセスなし】 - ソース表示かbotの疑い
\n" . "Host : " . $host . "
\n" . "IP : " . htmlspecialchars($ip) . "
\n" . "経過時間 : " . $diff . " 秒

\n" . "Path : /
\n" . "日時 : " . htmlspecialchars($root['date']) . "
\n" . "User-Agent : " . htmlspecialchars($root['userAgent']) . "

\n" . "結果 : /1x1.gif のアクセスなし
\n" . "
\n" ]; } usort($results, function($a, $b) { return $a['timestamp'] <=> $b['timestamp']; }); foreach ($results as $r) { echo $r['html']; } fclose($fp); ?>