title: How to return an HTTP 500 code on any error, no matter what
body: <p>I'm writing an authentication script in PHP, to be called as an API, that needs to return 200<code>only in the case that it approves the request, and</code>403<code>(Forbidden) or</code>500` otherwise.</p>
  
  <p>The problem I'm running into is that php returns <code>200</code> in the case of error conditions, outputting the error as html instead.  How can I make absolutely sure that php will return an HTTP <code>500</code> code unless I explicitly return the HTTP <code>200</code> or HTTP <code>403</code> myself?  In other words, I want to turn any and all warning or error conditions into <code>500</code>s, no exceptions, so that the default case is rejecting the authentication request, and the exception is approving it with a <code>200</code> code.</p>
  
  <p>I've fiddled with <code>set_error_handler()</code> and <code>error_reporting()</code>, but so far no luck.  For example, if the code outputs something before I send the HTTP response code, PHP naturally reports that you can't modify header information after outputting anything.  However, this is reported by PHP as a <code>200</code> response code with html explaining the problem.  I need even this kind of thing to be turned into a <code>500</code> code.</p>
  
  <p>Is this possible in PHP?  Or do I need to do this at a higher level like using <code>mod_rewrite</code> somehow?  If that's the case, any idea how I'd set that up?</p>