I have a strange problem with regards to a custom error handler function that deals with fatal errors. The error on my website is an "Fatal error: Allowed memory size of 134217728 bytes exhausted" - which of course I will fix. But I want a custom error page to show if it happens again.
The following code echo's number 1, but not my "error-page.php" and does not echo number 2. So it feels like the script is erroring on the file_get_contents line - but it's not telling me why.
To make matters worse - it echo's fine on my development server! But I cannot think of a set up difference that would effect this.
To try and solve this I have echo'd out the full path for the file_get_contents and it is correct - and file_exists returns true - so I am utterly stumped!
Worth noting - error-page.php is just a static HTML page so no offending code in that! It won't even file_get_contents a test.txt.
error_reporting(-1);
set_error_handler("customErrorPage");
register_shutdown_function('shutdownFunction');
function shutdownFunction()
{
$last_error = error_get_last();
if ($last_error['type'] === E_ERROR) {
fatalErrorHandler(E_ERROR, $last_error['message'], $last_error['file'], $last_error['line'], "");
}
}
function fatalErrorHandler($error_level,$error_message,$error_file,$error_line,$error_context) {
http_response_code(500);
$errorEmail = "<strong>Error on page:</strong> http://" . $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"] . "<br />
";
$errorEmail .= "<strong>Message:</strong> " . $error_message . "<br />
";
$errorEmail .= "<strong>IP:</strong> " . getRealIpAddr() . "<br />
";
$errorEmail .= "<strong>Line:</strong> " . $error_line . "<br />
";
$errorEmail .= "<strong>File:</strong> " . $error_file . "<br />
";
if ((strpos($_SERVER['SERVER_NAME'], ".com") > 0)) {
$to = "james@email.com";
$subject = "FATAL Error on " . $_SERVER["SERVER_NAME"] . " - " . $error_message;
$headers = "MIME-Version: 1.0'" . "
";
$headers .= "Content-type: text/html; charset=iso-8859-1". "
";
$headers .= "From: " . WEBSITE_COMPANY . " <errors@" . WEBSITE_DOMAIN . ">" . "
";
mail($to, $subject, $errorEmail, $headers);
echo "1";
echo file_get_contents($_SERVER["DOCUMENT_ROOT"] . "includes/error-page.php");
echo "2";
die;
} else {
echo $errorEmail;
die;
}
}
This is what I see on my screen when running the script:
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 14907 bytes) in /home/sure-wise/htdocs/objects/myaccount.php on line 2666 1