首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何从PHP脚本发送500内部服务器错误

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

Stack Overflow用户
提问于 2010-11-12 14:40:38
回答 5查看 144.3K关注 0票数 86

我需要发送"500内部服务器错误“从PHP脚本在某些条件下。该脚本应由第三方应用程序调用。该脚本包含两个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" );
?>

我只需要在执行最后一行之后发送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

这样的错误会被get服务器记录下来吗?

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2010-11-12 14:47:40

header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
票数 174
EN

Stack Overflow用户

发布于 2010-11-12 14:47:20

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

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);
    }
}

您可以这样使用它:

<?php
header_status(500);

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

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

update_database();

header_status(200);
票数 34
EN

Stack Overflow用户

发布于 2010-11-12 14:48:22

你可以直接放入:

header("HTTP/1.0 500 Internal Server Error");

在你的条件中,比如:

if (that happened) {
    header("HTTP/1.0 500 Internal Server Error");
}

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

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

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

票数 16
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4162223

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档