如何从PHP脚本发送500内部服务器错误错误

Sal*_*n A 73 php header http-headers

我需要在某些条件下从PHP脚本发送"500 Internal Server Error".该脚本应该由第三方应用程序调用.该脚本包含几个die("this happend")语句,我需要发送500 Internal Server Error响应代码而不是通常的200 OK.第三方脚本将在某些条件下重新发送请求,包括未收到200 OK响应代码.

问题的第二部分:我需要像这样设置我的脚本:

<?php
    custom_header( "500 Internal Server Error" );

    if ( that_happened ) {
        die( "that happened" )
    }

    if ( something_else_happened ) {
        die( "something else happened" )
    }

    update_database( );

    // the script can also fail on the above line
    // e.g. a mysql error occurred

    remove_header( "500" );
?>
Run Code Online (Sandbox Code Playgroud)

我需要200在最后一行执行后才发送标题.

编辑

一个附带问题:我可以发送奇怪的500个标题,例如:

HTTP/1.1 500 No Record Found
HTTP/1.1 500 Script Generated Error (E_RECORD_NOT_FOUND)
HTTP/1.1 500 Conditions Failed on Line 23
Run Code Online (Sandbox Code Playgroud)

这些错误是否会被网络服务器记录下来?

Cor*_*Xii 162

header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
Run Code Online (Sandbox Code Playgroud)

  • 如果标题不在开头(即工作证明),那就好了. (3认同)

inx*_*pro 40

PHP 5.4有一个名为http_response_code的函数,所以如果你使用的是PHP 5.4,你可以这样做:

http_response_code(500);
Run Code Online (Sandbox Code Playgroud)

如果您运行的是5.4以下的PHP版本,我已经为此函数(Gist)编写了一个polyfill.


要回答您的后续问题,HTTP 1.1 RFC说:

这里列出的原因只是建议 - 它们可以被本地等价物替换,而不会影响协议.

这意味着您可以在代码本身之后使用您想要的任何文本(不包括回车或换行符),并且它将起作用.但是,通常情况下,通常会有更好的响应代码.例如,代替使用500没有找到记录,你可以发送404(未找到),并且对于类似"条件失败"的东西(我猜测验证错误),你可以发送像422这样的东西(不可处理)实体).


And*_*ore 33

您可以使用以下函数发送状态更改:

function header_status($statusCode) {
    static $status_codes = null;

    if ($status_codes === null) {
        $status_codes = array (
            100 => 'Continue',
            101 => 'Switching Protocols',
            102 => 'Processing',
            200 => 'OK',
            201 => 'Created',
            202 => 'Accepted',
            203 => 'Non-Authoritative Information',
            204 => 'No Content',
            205 => 'Reset Content',
            206 => 'Partial Content',
            207 => 'Multi-Status',
            300 => 'Multiple Choices',
            301 => 'Moved Permanently',
            302 => 'Found',
            303 => 'See Other',
            304 => 'Not Modified',
            305 => 'Use Proxy',
            307 => 'Temporary Redirect',
            400 => 'Bad Request',
            401 => 'Unauthorized',
            402 => 'Payment Required',
            403 => 'Forbidden',
            404 => 'Not Found',
            405 => 'Method Not Allowed',
            406 => 'Not Acceptable',
            407 => 'Proxy Authentication Required',
            408 => 'Request Timeout',
            409 => 'Conflict',
            410 => 'Gone',
            411 => 'Length Required',
            412 => 'Precondition Failed',
            413 => 'Request Entity Too Large',
            414 => 'Request-URI Too Long',
            415 => 'Unsupported Media Type',
            416 => 'Requested Range Not Satisfiable',
            417 => 'Expectation Failed',
            422 => 'Unprocessable Entity',
            423 => 'Locked',
            424 => 'Failed Dependency',
            426 => 'Upgrade Required',
            500 => 'Internal Server Error',
            501 => 'Not Implemented',
            502 => 'Bad Gateway',
            503 => 'Service Unavailable',
            504 => 'Gateway Timeout',
            505 => 'HTTP Version Not Supported',
            506 => 'Variant Also Negotiates',
            507 => 'Insufficient Storage',
            509 => 'Bandwidth Limit Exceeded',
            510 => 'Not Extended'
        );
    }

    if ($status_codes[$statusCode] !== null) {
        $status_string = $statusCode . ' ' . $status_codes[$statusCode];
        header($_SERVER['SERVER_PROTOCOL'] . ' ' . $status_string, true, $statusCode);
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以这样使用它:

<?php
header_status(500);

if (that_happened) {
    die("that happened")
}

if (something_else_happened) {
    die("something else happened")
}

update_database();

header_status(200);
Run Code Online (Sandbox Code Playgroud)

  • 保存几个字节的内存很少是以某种方式编写Web应用程序代码的好理由. (21认同)
  • 您的列表缺少 418 =&gt;“我是茶壶”请参阅:https://en.wikipedia.org/wiki/Hyper_Text_Coffee_Pot_Control_Protocol (3认同)

Rue*_*uel 15

你可以放:

header("HTTP/1.0 500 Internal Server Error");
Run Code Online (Sandbox Code Playgroud)

在你的条件如下:

if (that happened) {
    header("HTTP/1.0 500 Internal Server Error");
}
Run Code Online (Sandbox Code Playgroud)

至于数据库查询,你可以这样做:

$result = mysql_query("..query string..") or header("HTTP/1.0 500 Internal Server Error");
Run Code Online (Sandbox Code Playgroud)

您应该记住,您必须将此代码放在任何html标记(或输出)之前.

  • 确保在调用header()后退出/ die/return/something.PHP将继续执行代码 - 这可能是不可取的. (11认同)

Dav*_*dža 8

您可以像这样简化它:

if ( that_happened || something_else_happened )
{
    header('X-Error-Message: Incorrect username or password', true, 500);
    die;
}
Run Code Online (Sandbox Code Playgroud)

它将返回以下标题:

HTTP/1.1 500 Internal Server Error
...
X-Error-Message: Incorrect username or password
...
Run Code Online (Sandbox Code Playgroud)

补充:如果您需要确切知道出了什么问题,请执行以下操作:

if ( that_happened )
{
    header('X-Error-Message: Incorrect username', true, 500);
    die('Incorrect username');
}

if ( something_else_happened )
{
    header('X-Error-Message: Incorrect password', true, 500);
    die('Incorrect password');
}
Run Code Online (Sandbox Code Playgroud)

  • 现在`'x'应该是什么? (5认同)