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?