특정 조건에서 PHP 스크립트에서 “500 내부 서버 오류”를 보내야합니다. 스크립트는 타사 앱에서 호출해야합니다. 스크립트에는 일반적인 대신 응답 코드 die("this happend")를 보내야 하는 몇 가지 문이 포함되어 있습니다 . 제 3 자 스크립트는 응답 코드를 받지 않는 것을 포함한 특정 조건에서 요청을 다시 보냅니다 .500 Internal Server Error200 OK200 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
이러한 오류가 웹 서버에 기록됩니까?
답변
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
답변
PHP 5.4에는 http_response_code 라는 함수가 있으므로 PHP 5.4를 사용하는 경우 다음을 수행 할 수 있습니다.
http_response_code(500);
5.4에서 PHP 버전을 실행하는 경우이 함수 (Gist)에 대한 polyfill을 작성했습니다 .
후속 질문에 답하기 위해 HTTP 1.1 RFC는 다음과 같이 말합니다.
여기에 나열된 이유 문구는 권장 사항 일 뿐이며 프로토콜에 영향을주지 않고 로컬 등가물로 대체 할 수 있습니다.
즉, 코드 자체 뒤에 원하는 텍스트 (캐리지 리턴 또는 줄 바꿈 제외)를 사용할 수 있으며 작동합니다. 하지만 일반적으로 더 나은 응답 코드를 사용할 수 있습니다. 예를 들어 레코드를 찾을 수없는 경우 500 을 사용하는 대신 404 (찾을 수 없음)를 보낼 수 있고 “조건 실패”(유효성 검사 오류를 추측 함)와 같은 경우에는 422 (처리 할 수 없음)와 같은 메시지를 보낼 수 있습니다. 실재).
답변
다음 기능을 사용하여 상태 변경을 보낼 수 있습니다.
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);
답변
다음을 넣을 수 있습니다.
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 태그 (또는 출력) 앞에이 코드를 넣어야한다는 것을 기억해야합니다.
답변
다음과 같이 단순화 할 수 있습니다.
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');
}
답변
코드는 다음과 같아야합니다.
<?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');
?>
뭔가 잘못되면 실행을 중지한다고 가정합니다.