代码之家  ›  专栏  ›  技术社区  ›  Salman Arshad

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

  •  70
  • Salman Arshad  · 技术社区  · 14 年前

    在某些情况下,我需要从PHP脚本发送“500内部服务器错误”。脚本应该由第三方应用程序调用。这个脚本包含两个 die("this happend") 我需要发送的声明 500 Internal Server Error 响应代码而不是通常的 200 OK . 第三方脚本将在某些条件下重新发送请求,包括不接收 200行 响应代码。

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

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

    Web服务器会记录这些错误吗?

    6 回复  |  直到 6 年前
        1
  •  158
  •   Core Xii    14 年前
    header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
    
        2
  •  36
  •   inxilpro    11 年前
        3
  •  32
  •   Andrew Moore    14 年前

    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);
    
        4
  •  15
  •   Ruel    14 年前

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

        5
  •  8
  •   Salman Arshad    12 年前

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

    HTTP/1.1 500 Internal Server Error
    ...
    X-Error-Message: Incorrect username or password
    ...
    

    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');
    }
    
        6
  •  2
  •   Uwe Keim    6 年前

    <?php
    if ( that_happened ) {
        header("HTTP/1.0 500 Internal Server Error");
        die();
    }
    
    if ( something_else_happened ) {
        header("HTTP/1.0 500 Internal Server Error");
        die();
    }
    
    // Your function should return FALSE if something goes wrong
    if ( !update_database() ) {
        header("HTTP/1.0 500 Internal Server Error");
        die();
    }
    
    // the script can also fail on the above line
    // e.g. a mysql error occurred
    
    
    header('HTTP/1.1 200 OK');
    ?>
    

    推荐文章