如何从 PHP 脚本发送 500 Internal Server Error 错误
I need to send "500 Internal Server Error" from an PHP script under certain conditions. The script is supposed to be called by a third party app. The script contains a couple of die("this happend")
statements for which I need to send the 500 Internal Server Error
response code instead of the usual 200 OK
. The third party script will re-send the request under certain conditions which include not receiving the 200 OK
response code.
Second part of the question: I need to setup my script like this:
<?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" );
?>
I need to send 200
header only after the last line has been executed.
Edit
A side question: can I send strange 500 headers such as these:
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
Will such errors get logged by the webserver?
解决方案header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
相关文章