Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I have PHP put up a nice error page for every error. With (very minor) trickery, you can even put up nice pages for fatal errors. Unless it's a database connection error, I have the site automatically email me a full stack trace and all other relevant details. You don't even have to turn off error notification in the PHP.INI file, you can control that from your script.


I believe your trickery involves capturing the output buffer and grep'ing for a fatal error, yes? We thought of doing that, but our site commands too much traffic for that to be a feasible option. If it's something less intrusive, I'm all ears.


You can't capture the output buffer on a fatal error; the script just aborts. You do have to wait till the end of your script to write your output otherwise this doesn't work very well but here is some (simplified) code:

  $errorTemplate = file_get_contents("errortemplate.html");
  $templates = explode('%error%', $text, 2);
  ini_set('error_prepend_string', $templates[0]);
  ini_set('error_append_string', $templates[1]);
  ini_set('html_errors', '0');	
The errortemplate.html file is a static HTML file that can contain anything you want. Ours is themed up just like any other page on our website. When a fatal error is triggered, the text %error% will be replaced with the fatal error text. We place that into a form using a hidden textarea so our users can submit it to us.

  <textarea style="display: none;">%error%</textarea>
The server log will contain the fatal error, so you don't care about users submitting it you can just wrap it in a comment.

  <!-- %error% -->
And that's all there is to it.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: