Posts Tagged ‘server’

Auto-Email Web Server Errors with WordPress

April 30, 2009

404/403 Website Error Pages with PHP Auto-Mailer from Nettuts+ scrolled through my RSS feeds the other day.  I was thinking that since I have WordPress installed it should be really easy to add some auto-emailing to the 404 template page.  While there are a few 404 WordPress plugins I didn’t see any that added the emailing.  So I added this to the bottom of my 404.php file:

<?php
$s_headers = "";
$s_to = "" ;
$s_subject = "404 Page Generated" ;
$s_message = "<ul>" ;
$s_message .= "<li>Request URL:" . $_SERVER['REQUEST_URI'] . "</li>";
$s_message .= "<li>Request Method:" . $_SERVER['REQUEST_METHOD'] . "</li>";
$s_message .= "<li>Referer:" . $_SERVER['HTTP_REFERER'] . "</li>";
$s_message .= "<li>Remote Address:" . $_SERVER['REMOTE_ADDR'] . "</li>";
$s_message .= "</ul>";
mail($s_to, $s_subject, $s_message, $s_headers ) ;
?>

So when a 404 gets generated I now am informed what failed, where it came from, who it came from and if it was a GET or POST.

What about other errors?

Ok, that was easy for the 404 because it was built into the WP template system.  But what about other errors like 500 (server error) and 403 (forbidden)?  WP doesn’t have templates for them.  Well, its pretty easy to take an arbitrary PHP script and include the entire WP environment.  I created a 500.php file that looks something like this:

<?php
define('WP_USE_THEMES', true);
require_once('wp-load.php');
?>
<?php get_header(); ?>
<div id="content" class="narrowcolumn">
<h2 class="center">Server Error - Pray For Us</h2>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
<?php /* all that emailing code I just wrote in the 404.php file but used "500" instead of 404 */ ?>

The define() and require_once() functions pull in the WordPress environment.  You only need to watch out for the relative path to the wp-load.php file since it depends on where your PHP script is executing.  Now, duplicate code is a poor programming practice and I had access to all of WP; that means I had access to the functions.php file for the current template.  So, I took the original emailing code and wrote it into a function in the functions.php file.  Of course, if I change the theme I’ll have to make sure this function moves also.  The alternative is to put it into a separate PHP script and include it like the wp-load.php script.  The last line of the 404.php and 500.php files now look like:

<?php mail_request_info( "404 Page" ) ; ?>

and

<?php mail_request_info( "500 Page" ) ; ?>

Wrapping Up

You’ll want to follow their tutorial for how to update your Apache environment to reference the 500.php in the event of a server error.  I also wrote a 403.php file that worked the same way.  This effort paid immediate dividends for me as I found a couple of minor broken links on the site.  Nothing an HTML validator wouldn’t have found but I don’t run one against the site unless I’m redesigning it.  Between these scripts and the Redirection Plugin there are now very few broken pages since the major redesign on the site.  One note of caution, if you’re a high-volume site you might get flooded with emails.

Advertisements