MrG*_*MrG 5 php apache custom-error-pages
好吧,我想我快要失去理智了......
一直在尝试和测试,但似乎无法加载自定义 HTTP 500 错误页面。Chrome 一直向我提供默认的“此页面无法正常工作,HTTP 错误 500”错误页面。
我已采取的步骤:
访问日志
:1 - - [10/8/2018:20:51:39 +0200]“GET / HTTP/1.1”500 -
错误日志
[Fri Aug 10 20:51:39.156104 2018] [php7:error] [pid 11608] [client ::1:65263] PHP 致命错误:未捕获错误:在 /private/var/www/development 中找不到类“tests” /public_html/index.php:7\n堆栈跟踪:\n#0 {main}\n 在第 7 行 /private/var/www/development/public_html/index.php 中抛出
.htaccess 行
ErrorDocument 400 /404.php
ErrorDocument 403 /403.php
ErrorDocument 404 /404.php
ErrorDocument 405 /405.php
ErrorDocument 408 /408.php
ErrorDocument 500 /500.php
ErrorDocument 502 /502.php
ErrorDocument 504 /504.php
Run Code Online (Sandbox Code Playgroud)
阿帕奇 2.4+ PHP 7+
我在这里没有看到这个问题,特别是因为上面的 404 版本运行完美。500.php 仅包含 echo '500';
我在这里缺少一些 Apache 设置吗?是因为是本地的吗...
你的评论基本上是正确的。许多 500 错误不会以.htaccess能够重定向到错误文档的方式到达 apache。
您可以通过两种方式为 5xx 错误提供自定义模板。您使用哪一个将取决于错误的具体内容。如果错误是“ Catchable ”,您只需将函数包装在一个try/catch块中即可。像这样的东西:
try{
someUndefinedFunction();
} catch (Throwable $exception) { //Use Throwable to catch both errors and exceptions
header('HTTP/1.1 500 Internal Server Error'); //Tell the browser this is a 500 error
echo $e->getMessage();
}
Run Code Online (Sandbox Code Playgroud)
请注意,在此示例中,必须手动设置 500 错误标头。这是因为,由于错误位于try{}块内,因此从浏览器的角度来看,服务器实际上并未出错。
如果 500 错误是由不可捕获的原因引起的,那么您需要注册一个自定义关闭函数。这在 php7+ 中不太常见,但仍然可能是必要的,具体取决于您正在做什么。完成的方法是包括这样的内容:
function handle_fatal_error() {
$error = error_get_last();
if (is_array($error)) {
$errorCode = $error['type'] ?? 0;
$errorMsg = $error['message'] ?? '';
$file = $error['file'] ?? '';
$line = $error['line'] ?? null;
if ($errorCode > 0) {
handle_error($errorCode, $errorMessage, $file, $line);
}
}
}
function handle_error($code, $msg, $file, $line) {
echo $code . ': '. $msg . 'in ' . $file . 'on line ' . $line;
}
set_error_handler("handle_error");
register_shutdown_function('handle_fatal_error');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6447 次 |
| 最近记录: |