我需要发送"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服务器记录下来吗?
发布于 2010-11-12 14:47:40
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
发布于 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);
发布于 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标记(或输出)之前。
https://stackoverflow.com/questions/4162223
复制相似问题