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?
register_shutdown_function() should never generate content viewable to the user. It is not intended for that purpose. 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:
C.