Days since last fatal error

I've had an idea tickling the back of my head for a while, remembering the Simpsons opening credits with Days Since Last Accident being changed. It's a common schtick, cartoons have had it for ages. I wondered, could I do that with my site's code. Turns out, I could. You can see it on my Code page now, right at the bottom. How does it work?

  1. Create a file to hold a shutdown function. This is called whenever a PHP script execution ends, even when out-of-memory or other instances occur.
  2. In the file:
    1. Register the shutdown function register_shutdown_function('fatalErrorShutdownHandler')
    2. Create the shutdown function. If the type of error_get_last is E_ERROR then call a function to draw the error ticker
    3. Create a function to draw the ticker as an SVG. It takes a paramter of how many days to draw, or null to read the current value and add one to it. This increases the number of days. Add a meta-field in there with the current value to make updating easier.
  3. Create another file that is called via crontab every day, to call the ticker-drawer with a null paramter.
  4. Update your php.ini to include the shutdown function file as auto_prepend_file so it is added to every PHP execution on the server (that uses that php.ini file anyway).

Since my page is mostly static content nowadays, I'm happy to have this tiny function prepended to every call. I'd probably think more carefully on it if my site was WordPress... or had traffic of any kind... but I get to use this to my advantage here.