如何让 PHP 在出现任何错误情况时自动将 HTTP 状态代码设置为 500? (包括用户无法处理的)
我正在运行 Apache 2.2.15 和 PHP 5.3.2,禁用“display_errors”,禁用“display_startup_errors”,启用“log_errors”。
在我的设置中(所以我认为这是一种常态),PHP 在出现致命错误时中止,这很好,并将 HTTP 状态代码设置为 500。致命错误包括 E_ERROR、E_PARSE、E_CORE_ERROR、E_COMPILE_ERROR、E_USER_ERROR 以及可能的 E_RECOVERABLE_ERROR(无法自己触发它,因此无法轻松检查发生了什么)。我认为将代码设置为 500 是个好主意,因为我认为这是正确的做法 - 显然,如果您的脚本包含语法错误和/或无法执行运行时应该执行的操作,那么它是如果我们将 PHP 视为服务器的一部分,则会出现服务器错误。
现在,这是重要的部分:
无论如何,我现在已经安装了 XDebug 来更好地跟踪错误,但我现在可以看到,无论错误是什么,即使脚本像以前一样因致命错误而中止, HTTP 状态代码始终为 200。这破坏了我的客户端通过 HTTP 与 Apache/PHP 进行“对话”的情况:|
此外,将 display_errors 设置为 On/1 将使 PHP 不再将 HTTP 状态代码设置为 500,并且表现出与上面的 XDebug 完全相同的行为。
我非常依赖这里可靠的状态代码行为,这一切都让我相信这是某种侥幸或像天气一样随机......或者我错过了什么?
更新
有一篇博客文章概述了该问题: http://talideon.com/weblog/2008/02/php-errors。 cfm
就我而言,我已禁用 XDebug,因为它首先导致了不良行为。无论如何,我只将它用于堆栈跟踪,现在使用自定义错误处理程序来代替。另外,链接的文章是 2008 年的,显然 PHP 现在确实自动将 HTTP 状态代码设置为 500。这里也是如此。当然,没有 XDebug。
I am running Apache 2.2.15 with PHP 5.3.2, 'display_errors' disabled, 'display_startup_errors' disabled, 'log_errors' enabled.
At my setup (so I consider it a norm), PHP aborts on fatal errors, which is good, and sets HTTP status code to 500. Fatal errors include E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR and, probably, E_RECOVERABLE_ERROR (cannot trigger it myself, so can't easily check what happens). I think it is a good idea that it does set the code to 500, because I think it is the right thing to do - obviously if your script contains syntax errors and/or fails to do what is supposed to do at runtime, it is a server error, if we consider PHP part of the server.
Now, here is the important part:
Anyway, I have now installed XDebug to track errors better, but I can see that now, no matter the error, even though the script aborts as before on fatal errors, the HTTP status code is always 200. This breaks my client that 'talks' to Apache/PHP via HTTP :|
Also, setting display_errors to On/1, makes PHP no longer set HTTP status code to 500 and exhibits exactly the same behavior as with XDebug above.
I am very much dependent on reliable status code behavior here, and this all leads me to believe it's some kind of fluke or random like weather.. or am I missing something?
UPDATE
There is a blog post which oulines the issue:
http://talideon.com/weblog/2008/02/php-errors.cfm
For my part, I have disabled XDebug, seeing as it is what causes the bad behavior in the first place. I only used it for stack tracing anyway, and now use a custom error handler for that instead. Also, the linked article is from 2008, apparently PHP does set HTTP status code to 500 automatically these days. It does so here. Without XDebug, of course.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设您正在使用自定义错误处理程序来发出 500。
我不太了解 XDebug,但根据 这篇文章,它注册了自己的错误处理程序,可能会在此过程中覆盖您的错误处理程序:
但是,对于生产用途,您无论如何都不会激活 XDebug,不是吗?
至于为什么激活
display_errors()
时会输出200,我不明白。您可以发布您的自定义错误处理函数来查看吗?I assume you are using a custom error handler to emit the 500.
I don't know XDebug that well, but according to this article, it registers its own error handler, probably overriding yours in the process:
However, for production use, you are not going to activate XDebug anyway, are you?
As for why a 200 is output when you activate
display_errors()
, that I don't understand. Can you post your custom error handler function to look at?