如果我意外地在代码中犯了错误,是否可以使用PHP显示500个页面?我试着使用error_reporting(0),但那只会隐藏错误。如果我使用htaccess的php_flag display_errors off,Chrome (和其他浏览器)会像这样显示一个500错误:http://image.prntscr.com/image/4c87df1998634097a18a85d268ccc818.png
谢谢:)
发布于 2016-08-03 22:55:12
在PHP5.x中,你可以捕获除致命错误之外的所有错误。看看http://php.net/manual/en/function.set-error-handler.php吧
快速示例:
<?php
function handler($errno, $errstr, $errfile, $errline)
{
echo file_get_contents('500.html');
die();
}
set_error_handler('handler', E_ALL);发布于 2016-08-03 23:01:24
组合这些函数以在出错时进行重定向:
error_get_last() #获取lasterror
如果需要,重定向的header() #
使用error_get_last捕获错误的register_shutdown_function() #
ob_start() / ob_flush()#捕获内容,用于后期显示或不显示
在php文件的开头:
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;
}
});您还可以使用此命令捕获致命错误。
发布于 2016-08-03 22:54:33
任何let服务器(apache/nginx/lighttpd)都可以让你定制错误页面
https://httpd.apache.org/docs/2.4/custom-error.html
http://redmine.lighttpd.net/boards/2/topics/4639
但不要忘记关闭PHP错误显示,只需记录它们。
https://stackoverflow.com/questions/38746828
复制相似问题