Reputation: 110
Is it possible, using PHP, to show a 500 page if I accidently make a mistake in my code? I tried using error_reporting(0)
but that will just hide the errors. And if I use htaccess' php_flag display_errors off
Chrome (and other browsers) will just display a 500 error like this one: http://image.prntscr.com/image/4c87df1998634097a18a85d268ccc818.png
Thanks :)
Upvotes: 0
Views: 103
Reputation: 3795
Combianate these functions to make a redirect on error:
error_get_last()
# to get the lasterror
header()
# to rediect if needed
register_shutdown_function()
# to catch errors with error_get_last
ob_start() / ob_flush()
# catch content, for late showing or not
At start of your php file:
ob_start();
register_shutdown_function(function(){
$err = error_get_last();
//check the $err
if($err){
header('Location: 505.html');
exit;
} else {
ob_flush();#or ob_end_flush();
exit;
}
});
You can also catch fatal errors with this.
Upvotes: 2
Reputation: 46
In PHP 5.x you can catch all errors except fatals. Just look at http://php.net/manual/en/function.set-error-handler.php
Quick example:
<?php
function handler($errno, $errstr, $errfile, $errline)
{
echo file_get_contents('500.html');
die();
}
set_error_handler('handler', E_ALL);
Upvotes: 2
Reputation: 3144
Any webserver (apache/nginx/lighttpd) let you do custom error pages
https://httpd.apache.org/docs/2.4/custom-error.html
http://redmine.lighttpd.net/boards/2/topics/4639
But do not forget to turn off PHP error display, just log them.
Upvotes: 0