如何让 php 在遇到致命异常时返回 500?
PHP 致命错误以状态代码 200 返回 HTTP 客户端。如何让它返回状态代码 500(内部服务器错误)?
PHP fatal errors come back as status code 200 to the HTTP client. How can I make it return a status code 500 (Internal server error)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
另请参阅此回答
Also, see this answer
这正是我昨天遇到的问题,我找到了解决方案如下:
首先,您需要捕获 PHP 致命错误,这是错误类型 E_ERROR。当此错误发生时,脚本将存储错误并终止执行。您可以通过调用函数error_get_last()来获取存储的错误。
在脚本终止之前,总会调用回调函数register_shutdown_function()。因此,您需要通过此函数注册一个错误处理程序来执行您想要的操作,在本例中,返回标头 500 和自定义的内部错误页面(可选)。
函数 my_error_handler()
{
$last_error = error_get_last();
if ($last_error && $last_error['type']==E_ERROR)
{
header("HTTP/1.1 500 内部服务器错误");
echo '...';//500页的html
}
}
register_shutdown_function('my_error_handler');
注意:如果要捕获以E_USER*开头的自定义错误类型,可以使用函数set_error_handler()注册错误处理程序并通过函数trigger_error触发错误,但是该错误处理程序无法处理E_ERROR错误类型。请参阅 php.net 上有关 错误处理程序 的说明
This is exactly the problem I had yesterday and I found solution as follows:
first of all, you need to catch PHP fatal errors, which is error type E_ERROR. when this error occurs, script will be stored the error and terminate execution. you can get the stored error by calling function error_get_last().
before script terminated, a callback function register_shutdown_function() will always be called. so you need to register a error handler by this function to do what you want, in this case, return header 500 and a customized internal error page (optional).
function my_error_handler()
{
$last_error = error_get_last();
if ($last_error && $last_error['type']==E_ERROR)
{
header("HTTP/1.1 500 Internal Server Error");
echo '...';//html for 500 page
}
}
register_shutdown_function('my_error_handler');
Note: if you want to catch custom error type, which start with E_USER*, you can use function set_error_handler() to register error handler and trigger error by function trigger_error, however, this error handler can not handle E_ERROR error type. see explanation on php.net about error handler
标准 PHP 配置确实在发生错误时返回 500!只需确保您的 display_errors = 关闭即可。您可以通过以下方式模拟它:
在生产中,默认情况下,display_errors 指令处于关闭状态。
Standard PHP configuration does return 500 when error occurs! Just make sure that your display_errors = off. You can simulate it with:
On production display_errors directive is off by default.
由于 PHP >= 5.4
请在
echo
之前设置 httpCode。Since PHP >= 5.4
Please set the httpCode before
echo
.我使用“set_exception_handler”来处理未捕获的异常。
I have used "set_exception_handler" to handle uncaught exceptions.
根据 PHP 文档,不可能以任何方式处理 PHP E_ERROR:
http://www.php.net/manual/en/ function.set-error-handler.php
根据该链接,也无法处理“E_PARSE、E_CORE_ERROR、E_CORE_WARNING、E_COMPILE_ERROR、E_COMPILE_WARNING 和大部分 E_STRICT”。
您可以为其他错误、警告和通知(包括 E_USER_ERROR)提供处理程序,但这实际上并不像听起来那么有用,因为该错误只是由程序员使用trigger_error() 故意抛出的。
当然,您可以捕获任何异常(甚至是本机 PHP 函数抛出的异常)。
我同意这是一个问题。当应用程序代码崩溃并烧毁时,服务器不应返回 200 OK。
It is not possible to handle PHP E_ERROR in any way according to the PHP documentation:
http://www.php.net/manual/en/function.set-error-handler.php
Nor is is possible to handle "E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and most of E_STRICT" according to that link.
You CAN provide a handler for the other error, warning, and notices including E_USER_ERROR, but that's really not as useful as it sounds since this error only gets thrown intentionally by the programmer with trigger_error().
And of course you can catch any Exception (even the ones thrown by the native PHP functions).
I agree that this is a problem. Servers should NOT return 200 OK when application code crashes and burns.
您可以使用 php 错误处理
http://www.w3schools.com/php/php_error.asp< /a>
You can use php error handling
http://www.w3schools.com/php/php_error.asp
您必须使用 try/catch 捕获抛出的错误,然后使用它catch 块发送带有 500 错误的 header() 。
如果致命异常没有被 try {} catch 块包围,那么您必须注册一个全局处理程序并使用
register_shutdown_function()
在脚本末尾检查错误。You would have to catch the thrown error using try/catch and then use that catch block to send a header() with the 500 error.
If the fatal exception is not surrounded by try {} catch blocks then you must register a global handler and use
register_shutdown_function()
to check for an error at script end.永远不要忘记将
header("HTTP/1.1 200 OK", true, 200);
设置为任何执行路径的最后一行:在
PHP 5.4
中,您可以替换 <上面的 code>header 函数具有更好的http_response_code(200)
或 <代码>http_response_code(500)。Never forget to set
header("HTTP/1.1 200 OK", true, 200);
as the last line of any execution path:In
PHP 5.4
you can replace theheader
function above with the much betterhttp_response_code(200)
orhttp_response_code(500)
.处理致命错误(编译错误,例如缺少分号)时最困难的事情是脚本不会被执行,因此在该脚本中设置状态代码无济于事。但是,当您包含或需要脚本时,无论包含的脚本中是否存在错误,都将执行调用脚本。有了这个,我得出了这个解决方案:
rock-solid-script.php:
script-i-want-to-guard-for-errors.php:
Direct your call to the rock-solid-script.php 就准备好了去。
我希望最好将 .htaccess 中的默认状态代码设置为 500。这对我来说似乎更优雅,但我找不到一种方法来实现它。我尝试了 RewriteRule R 标志,但这完全阻止了 php 的执行,所以没有用。
The hard thing when dealing with fatal errors (compile errors, for example a missing semicolon) is that the script won't be executed, so it won't help to set the status code in that script. However, when you include or require a script, the calling script will be executed, regardless of errors in the included script. With this, I come to this solution:
rock-solid-script.php:
script-i-want-to-guard-for-errors.php:
Direct your call to the rock-solid-script.php and you're ready to go.
I would have liked it better to set the default status code to 500 in .htaccess. That seems more elegant to me but I can't find a way to pull it off. I tried the RewriteRule R-flag, but this prevents execution of php altogether, so that's no use.