views:

30

answers:

1

Hi!

I'm developing a PHP application that uses HTTP response codes for communication as well as response bodies.

So this is a typical scenario in the PHP code:

   try {
       doSomething();
   } catch(Exception $e) {
       header('HTTP/1.1 500 Internal Server Error');
   }

... and the typical code in clients looks like:

switch(responseCode) {

     case 200 :
         // all fine

         // ....

     case 500 :
         // trouble!

 } 

This is cool, as long as every error is well catched in PHP code.

The problem: If, for any reason in the php code occures an uncatched error, apache will send 200 OK. But I wan't apache to say 500. Maybe per .htaccess or so.

Any suggestions?

+1  A: 

You can, of course, write your own error handler. However, not all PHP errors are catchable. For instance, a syntax error won't even allow your code to run, including your error handler.

To handle catchable errors, you can use the auto_append_file and auto_prepend_file directives to place your error handling code code.

Non-catchable errors are a different issue. If PHP runs as Fast CGI, it will automatically generate a 500 status code for you. However, you're probably running PHP through some other SAPI (such as Apache module). No idea about that, sorry. (I'll report back if I find something.)

Álvaro G. Vicario
Thanks for the quick answer! Yes I wrote a custom error- and exception handler, but you are right, the uncatchable errors are the problem. I'm googling on this..
thorsten
Whatever, it's sort of uncommon that such kind of fatal errors go live unnoticed, isn't it?
Álvaro G. Vicario
Unfortunately not:class Test { protected $yntaxError}(forgot the ';')
thorsten
syntax errors and other fatal errors can't be caught by any error handler
stillstanding
Sure. Thatswhy I asked for a apache solution.
thorsten