title: How to send 500 Internal Server Error error from a PHP script
body: <p>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 <code>die("this happend")</code> statements for which I need to send the <code>500 Internal Server Error</code> response code instead of the usual <code>200 OK</code>. The third party script will re-send the request under certain conditions which include not receiving the <code>200 OK</code> response code.</p>
  
  <p>Second part of the question: I need to setup my script like this:</p>
  
  <pre><code>&lt;?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" );
  ?&gt;
  </code></pre>
  
  <p>I need to send <code>200</code> header only after the last line has been executed.</p>
  
  <h1>Edit</h1>
  
  <p>A side question: can I send strange 500 headers such as these:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Will such errors get logged by the webserver?</p>