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?
- 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.
- In the file:
- Register the shutdown function
register_shutdown_function('fatalErrorShutdownHandler')
- Create the shutdown function. If the type of
error_get_last
isE_ERROR
then call a function to draw the error ticker - 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.
- Register the shutdown function
- Create another file that is called via
crontab
every day, to call the ticker-drawer with anull
paramter. - Update your
php.ini
to include the shutdown function file asauto_prepend_file
so it is added to every PHP execution on the server (that uses thatphp.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.
Thoughts?
Sources
- https://insomanic.me.uk/php-trick-catching-fatal-errors-e-error-with-a-custom-error-handler-cea2262697a2 - to catch every shutdown, fjord every stream (damn, sorry), and see if it was an error or not.
- https://stackoverflow.com/questions/3792069/how-to-include-a-php-file-site-wide-using-htaccess-or-other-methods - to auto-append the exception handler
- http://planetphotoshop.com/flip-fonts-effect.html - to create the flip-character type effect