0

This is the code for my 404.php:

<?php
include $basedir  . 'includes/pageheader.php';
require dirname(__FILE__) . './vendor/autoload.php';
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

$username = isset($_SESSION['username']) ? trim($_SESSION['username']) : '';

//code in question
$log = new Logger('404.php');
$log->pushHandler(new StreamHandler(dirname(__FILE__) . './log/db_queries.log', Logger::ERROR));
// $log->error($error['message'] . '. SQL query which caused the error: ' . $error['sqltext']);
$log->error("404 Error: ", array('username' => $username, 'module' => $_SERVER['PHP_SELF'], 'page_name' => '404.php', 'error_at' => 'line 9', 'severity' => 'ERROR', 'error_msg' => '404 Bad Request. IP Address:' . "'" . $_SERVER['REMOTE_ADDR'] . "'" . 'Server name: ' . "'" . $_SERVER['SERVER_NAME'] . "'"));
// code in question

echo "404 page hit";

Problem is, this code is is running and executing a log entry in my log no matter what, even if a user never gets served this page. This page is displayed if the user gets a 404 error, which is triggered via my .htaccess page:

ErrorDocument 404 /wam/404.php

How do I stop this logging code from running no matter what and creating logs constantly? I only want the logging code to run if the user is actually diverted to the 404.php page.

This may be my ignorance as to how code is ran in php, it seems that all pages are rendered on the server no matter what? Even if the page is never requested/navigated to by a user?

Daniel L
  • 123
  • 8
  • this makes no sense, the code does not run itself. –  May 06 '19 at 23:15
  • That's what I can't understand, this log is being created even if this page is never hit. – Daniel L May 06 '19 at 23:21
  • 1
    check the access logs see what is actually hitting the file, but i bet you have the code elsewhere –  May 06 '19 at 23:48
  • That logging code is nowhere else. Though I am using logging elsewhere, that log starting with msg "404 Error: ", is definitely only in this file. Such a strange thing and so hard to find an answer for this. Every page that is correctly navigating to is creating this log from the 404.php, so log is getting filled with this error even if no 404 errors are happening. – Daniel L May 07 '19 at 00:00
  • I'm still stuck on this. I've commented out the monolog code on the 404 page and navigated around the app and it doesn't create 404 logs. So I know 100% this code from the 404 page is running and creating logs even though the 404 page is not being accessed. – Daniel L May 09 '19 at 00:32

0 Answers0