无论如何,如何在发生任何错误时返回 HTTP 500 代码
我正在用 PHP 编写一个身份验证脚本,作为 API 调用,仅在批准请求的情况下才需要返回 200,以及
403(禁止)或500`否则。
我遇到的问题是 php 在出现错误情况下返回 200
,而是将错误输出为 html。除非我自己明确返回 HTTP 200
或 HTTP 403
,否则如何绝对确保 php 将返回 HTTP 500
代码?换句话说,我想将任何和所有警告或错误条件转换为 500
,没有例外,因此默认情况是拒绝身份验证请求,例外情况是使用 批准它>200
代码。
我摆弄了 set_error_handler()
和 error_reporting()
,但到目前为止还没有运气。例如,如果代码在我发送 HTTP 响应代码之前输出了某些内容,PHP 自然会报告输出任何内容后无法修改标头信息。然而,PHP 将其报告为 200
响应代码,并用 html 解释了问题。我什至需要将这种东西变成 500
代码。
这在 PHP 中可能吗?或者我是否需要在更高的级别上执行此操作,例如使用 mod_rewrite
?如果是这样的话,你知道我该如何设置吗?
I'm writing an authentication script in PHP, to be called as an API, that needs to return 200only in the case that it approves the request, and
403(Forbidden) or
500` otherwise.
The problem I'm running into is that php returns 200
in the case of error conditions, outputting the error as html instead. How can I make absolutely sure that php will return an HTTP 500
code unless I explicitly return the HTTP 200
or HTTP 403
myself? In other words, I want to turn any and all warning or error conditions into 500
s, no exceptions, so that the default case is rejecting the authentication request, and the exception is approving it with a 200
code.
I've fiddled with set_error_handler()
and error_reporting()
, but so far no luck. For example, if the code outputs something before I send the HTTP response code, PHP naturally reports that you can't modify header information after outputting anything. However, this is reported by PHP as a 200
response code with html explaining the problem. I need even this kind of thing to be turned into a 500
code.
Is this possible in PHP? Or do I need to do this at a higher level like using mod_rewrite
somehow? If that's the case, any idea how I'd set that up?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
只需发送状态代码作为响应
header()
< /a>:请记住,发送此消息时不得在其之前有任何输出。这意味着没有
echo
调用,也没有 HTML 或空白。Simply send the status code as a response
header()
:Remember that when sending this there must not be any output before it. That means no
echo
calls and no HTML or whitespace.我检查了 header() 的 PHP 文档,它比我简单正在制作 - 如果第二个参数为 true,它将替换类似的标头。默认为 true。所以正确的行为是 header
('HTTP/1.1 403 Forbidden');
,然后执行身份验证逻辑,然后如果通过身份验证,则执行 header('HTTP/1.1 200 OK')< /代码>。它将取代 403 响应,并保证 403 是默认响应。
I checked the PHP docs for header(), and it's simpler than I was making it - if the second parameter is true, it will replace a similar header. the default is true. So the correct behavior is header
('HTTP/1.1 403 Forbidden');
, then do the authentication logic, then if it authenticates, do header('HTTP/1.1 200 OK')
. It will replace the 403 response, and will guarantee that 403 is the default.从 PHP 5.4.0 开始,有一个专门的函数:
只需确保在任何其他输出之前调用它即可。
参考:
Since PHP 5.4.0 there is a specialized function:
Just make sure that it's called before any other output.
Reference:
在 set_error_handler() 的 php 页面上,您可以找到 smp 在 ncoastsoft dot com 于 2003 年 9 月 8 日 10:28 发表的评论,其中解释了如何捕获致命错误(通常无法使用自定义错误处理程序捕获这些错误)。我根据您的需要更改了代码:
这应该捕获不存在函数的致命错误,然后返回 500 并停止脚本其余部分的执行。
On the php page for set_error_handler() you can find a comment by smp at ncoastsoft dot com posted on 08-Sep-2003 10:28 which exlpains how to even catch fatal errors (which you can normally not catch with a custom error handler. I changed the code for you needs:
This shold catch the fatal error of the non existing function. It than returns a 500 and stops the execution of the rest of the script.
您不应使用 500,它表示内部服务器错误。
此(和其他标头)应在任何输出之前发送,除非启用了输出缓冲。
You should not use 500, that indicates an internal server error.
This (and other headers) should be sent before any ouput, except if you have output buffering enabled.
很好奇您是否知道如何让 PHP 在解析错误时返回 500...我有同样的需求...API 正在发布到我们的应用程序,并且只有在一切都正确处理的情况下才期望返回 200...
在这种情况下发生解析错误时,PHP 返回错误 200,我使用了这两个函数:
register_shutdown_function()
set_error_handler()
并且当代码中发生“解析/语法”错误时这些不会被命中...
所以这是一个低级 PHP 函数,必须在 PHP.INI 或其他地方设置...我正在使用 PHP 5.3.10...
Curious if you ever figured out how to get PHP to return 500 on Parse errors... I have the same exact need... API is posting to our app and expects a 200 only if everything was processed correctly...
In the case of a Parse error, PHP returns an Error 200 and I have used both of these functions:
register_shutdown_function()
set_error_handler()
and these don't get hit when a "parse/syntax" error happens in the code...
So this is a low-level PHP function that would have to be set in the PHP.INI or somewhere... I am using PHP 5.3.10...
使用输出缓冲允许您在写入页面正文后修改标题。您可以在 ini 文件 中进行设置而不是更新每个脚本。
(请注意,如果您显式刷新输出缓冲区,则 header() 调用仍然会失败)
C.
Use output buffering to allow you to modify the headers after writing to the body of the page. You can set this in the ini file rather than updating every script.
(Note the header() call will still fail if you explicitly flush the output buffer)
C.