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

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

这样的错误会被networking服务器logging吗?

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

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

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

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

 http_response_code(500); 

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


为了回答你的后续问题,HTTP 1.1 RFC说:

这里列出的短语仅仅是build议 – 它们可能被本地等价物取代而不影响协议。

这意味着你可以在代码本身之后使用任何你想要的文本(不包括回车符或换行符),它就可以工作。 不过,通常情况下,通常会有更好的响应代码来使用。 例如,可以发送一个404 (未find),而不是使用500找不到logging,而对于“条件失败”(我猜测validation错误)的情况,可以发送类似于422 (不可处理实体)。

你可以把:

 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 // eg a mysql error occurred header('HTTP/1.1 200 OK'); ?> 

如果出现问题,我假设你停止执行。