php 无论如何,如何在出现任何错误时返回 HTTP 500 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3052715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:32:43  来源:igfitidea点击:

How to return an HTTP 500 code on any error, no matter what

phphttpresponse

提问by Jake

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, and403(Forbidden) or500` otherwise.

我正在用 PHP 编写身份验证脚本,作为 API 调用,否则需要返回 200 only in the case that it approves the request, and403 (Forbidden) or500`。

The problem I'm running into is that php returns 200in the case of error conditions, outputting the error as html instead. How can I make absolutely sure that php will return an HTTP 500code unless I explicitly return the HTTP 200or HTTP 403myself? In other words, I want to turn any and all warning or error conditions into 500s, no exceptions, so that the default case is rejecting the authentication request, and the exception is approving it with a 200code.

200遇到的问题是 php在出现错误情况时返回,而是将错误输出为 html。500除非我自己明确返回 HTTP200或 HTTP,否则如何绝对确保 php 将返回 HTTP代码403?换句话说,我想把任何和所有警告或错误条件都变成500s,没有异常,这样默认情况是拒绝身份验证请求,异常是用200代码批准它。

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 200response code with html explaining the problem. I need even this kind of thing to be turned into a 500code.

我摆弄过set_error_handler()and error_reporting(),但到目前为止还没有运气。例如,如果代码在我发送HTTP响应代码之前输出了一些内容,那么PHP输出任何内容后自然会报告您无法修改头信息。但是,这是由 PHP 报告为200带有 html 解释问题的响应代码。我什至需要将这种东西变成500代码。

Is this possible in PHP? Or do I need to do this at a higher level like using mod_rewritesomehow? If that's the case, any idea how I'd set that up?

这在 PHP 中可能吗?或者我是否需要在更高级别上执行此操作,例如以mod_rewrite某种方式使用?如果是这样的话,知道我将如何设置吗?

采纳答案by Jake

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.

我检查了 header() 的 PHP 文档,它比我制作的更简单 - 如果第二个参数为真,它将替换类似的标题。默认为真。所以正确的行为是 header ('HTTP/1.1 403 Forbidden');,然后做身份验证逻辑,然后如果它通过身份验证,做 header ('HTTP/1.1 200 OK')。它将替换 403 响应,并保证 403 是默认值。

回答by BoltClock

Simply send the status code as a response header():

只需发送状态代码作为响应header()

header('HTTP/1.1 500 Internal Server Error');

Remember that when sending this there must notbe any output before it. That means no echocalls and no HTML or whitespace.

请记住,在发送它时,它之前不能有任何输出。这意味着没有echo调用,也没有 HTML 或空格。

回答by Martin M

Since PHP 5.4.0 there is a specialized function:

从 PHP 5.4.0 开始有一个专门的函数:

<?php http_response_code( 500 ); ?>

Just make sure that it's called before any other output.

只要确保它在任何其他输出之前被调用。

Reference:

参考:

回答by 2ndkauboy

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:

在 set_error_handler() 的 php 页面上,您可以在 ncoastsoft dot com 上找到 smp 发表于 08-Sep-2003 10:28 的评论,其中解释了如何甚至捕获致命错误(您通常无法使用自定义错误处理程序捕获这些错误。我根据您的需要更改了代码:

error_reporting(E_ALL);
ini_set('display_errors', 'on');

function fatal_error_handler($buffer) {
    header('HTTP/1.1 500 Internal Server Error');
    exit(0);
}

function handle_error ($errno, $errstr, $errfile, $errline){
    header('HTTP/1.1 500 Internal Server Error');
    exit(0);
}

ob_start("fatal_error_handler");
set_error_handler("handle_error");

//would normally cause a fatal error, but instead our output handler will be called allowing us to handle the error.
somefunction();
ob_end_flush();

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 并停止执行脚本的其余部分。

回答by Artefacto

header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden');

You should not use 500, that indicates an internal server error.

您不应使用 500,这表示内部服务器错误。

This (and other headers) should be sent before any ouput, except if you have output buffering enabled.

这个(和其他头)应该在任何输出之前发送,除非你启用了输出缓冲。

回答by Patrick Steil

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...

好奇你是否曾经想过如何让 PHP 在解析错误时返回 500 ......我有同样的需求...... API 正在发布到我们的应用程序,并且只有在一切都被正确处理的情况下才期望 200......

In the case of a Parse error, PHP returns an Error 200 and I have used both of these functions:

在解析错误的情况下,PHP 返回错误 200,我使用了这两个函数:

register_shutdown_function() set_error_handler()

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...

所以这是一个必须在 PHP.INI 或其他地方设置的低级 PHP 函数......我使用的是 PHP 5.3.10......

回答by symcbean

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 filerather than updating every script.

使用输出缓冲允许您在写入页面正文后修改标题。您可以在ini 文件中设置它而不是更新每个脚本。

(Note the header() call will still fail if you explicitly flush the output buffer)

(请注意,如果您显式刷新输出缓冲区,header() 调用仍然会失败)

C.

C。