tags:

views:

25

answers:

1

I use register_shutdown_function() to let PHP call a function at any time my script crashes. After I've logged that to a file, I want to display a beautiful error-sorry-message to the user.

To do that, I want to clean the current output buffer. I think there's a stack of output buffers (not sure), so the big question is if I could simply call ob_end_clean() in my shutdown-callback function and then print out my error page?

At least, on my MAMP environment on the mac (local) I can echo out something in my callback function, even though in the documentation some people claim that this is impossible.

But if that works, I must be sure that anything that went previously to the output buffer really gets cleaned. On the other hand, the next question would be what happens with sent headers?

A: 
register_shutdown_function() should never generate content viewable to the user. It is not intended for that purpose (RTFM). There is guarantee that the socket will be still be open - and if it is, the HTTP file stream may not be writeable.

If you want to handle errors more gracefully than the default then you need to write and install your own error handler:

 <?php

 set_error_handler('britney');

 function britney()
 {
    ob_clean;
    print "Whoops! I did it again<br />";
    debug_print_backtrace();
    ob_end_flush();
 }
 ..... your code goes here

C.

symcbean
what do you mean by "install"? I don't see anything that would have to be "installed" in your code. You're cleaning the current output buffer, writing stuff to it, add the back trace, and then flush it out again. Where's the difference now?
openfrog
You would need to ensure that the error_handler is declared and referenced as the error handler before any of your current codebase executes - i.e. you would either need to add an include at the top of every file or use an auto-prepend config setting - hence 'install'C.
symcbean