这是我的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";
问题是,即使用户从未获得此页面的服务,此代码也正在运行并无论如何在我的日志中执行日志条目。如果用户收到404错误(通过我的.htaccess页面触发),则会显示此页面:
ErrorDocument 404 /wam/404.php
无论如何,如何停止运行此日志记录代码并不断创建日志?我只希望如果实际上将用户转移到404.php页面,则运行日志记录代码。
这可能是我对如何在php中运行代码不了解,似乎所有页面都呈现在服务器上,无论如何?即使该页面从未被用户请求/导航过?