PDA

View Full Version : Custom 404 Error Page With Email Notification


IH-James
15-06-2004, 06:01 AM
This tutorial allows you (the site owner) to be notified whenever an error such as a 404 (File Not Found) error occurs.

This will work for all InfernoHost (http://www.infernohost.com.au) customers. If you aren't an InfernoHost customer, this will only work on an Apache web server that allows .htaccess (most do allow this). Also your web server must have PHP installed.

In the root directory of your site, create a file called ".htaccess" (without the quotes). This file should contain:
ErrorDocument 404 404.php
Now, in the same (root) directory, create a file called 404.php, with the following contents:
<?php
//email address to send the email to
$to = "youremail@address.com";

//subject of the email
$subject = "404 error at yoursite.com";

//who the email is from
$from = "user@domain.com";

//do not edit these two variables
$referring_url = $_SERVER['HTTP_REFERER'];
$requested_url = $_SERVER["REQUEST_URI"];

//The body of the email to be sent to you. You can leave this as is, or customise it if you want.
$body = "Requested URL: " . $requested_url . "\nReferring URL: " . $referring_url;

//Defines the sendMail() function
function sendMail($to, $subject, $body, $from) {
$from = trim($from);
$rp = 'ReturnNonWorkingHere@MySite.net';
$org = 'YourSite.com';
$mailer = 'YourSite.com Mailer';

$head = '';
$head .= "Content-Type: text/plain \r\n";
$head .= "Date: ". date('r'). " \r\n";
$head .= "Return-Path: $rp \r\n";
$head .= "From: $from \r\n";
$head .= "Sender: $from \r\n";
$head .= "Reply-To: $from \r\n";
$head .= "Organization: $org \r\n";
$head .= "X-Sender: $from \r\n";
$head .= "X-Priority: 3 \r\n";
$head .= "X-Mailer: $mailer \r\n";
$body = str_replace("\r\n", "\n", $body);
$body = str_replace("\n", "\r\n", $body);
return mail($to, $subject, $body, $head);
}
//Sends the email
sendMail($to, $subject, $body, $from);
?>
<html>
<head>
<title>404 Error!</title>
</head>
<body>
<p>The requested URL (<?php echo $requested_url ?>) was not found. A staff member has been notified of this error.</p>
<p>Please go <a href="<?php echo $referring_url ?>">back</a> and try again.</p>
</body>
</html>
There are a few variables in this file to edit, most of which should be obvious.

Once completed, upload both of these files to the root directory of your web server (if you already haven't done so).

If you now visit a URL to your site that isn't really there, you should see the error message (you can customize this by editing 404.php). You should then receive an email shortly after this containing the details of the error.

This tutorial can easily be adapted for other errors such as 400 (Bad Request), 401 (Authorization Required), 403 (Forbidden), and 500 (Internal Server Error). Just add new lines to the .htaccess file, then copy the 404.php and rename it to whatever error number you wish.

The source code for this tutorial can also be downloaded here:

IH-James
09-07-2004, 11:29 AM
If you found this tutorial useful, please rate it at HotScripts:
http://www.hotscripts.com/Detailed/35672.html

Comments/Suggestions? Feel free to reply to this thread (Guests must register in order to post).

hackula
14-10-2004, 06:51 AM
Thank you! I am glad you made this available, it is very handy to have live reports of 404s :)

One thing I did want to let you know: The zip file for downloading the files is corrupt.

Thanks again,

Mr. H.

IH-James
16-10-2004, 06:37 AM
Thank you! I am glad you made this available, it is very handy to have live reports of 404s :)

One thing I did want to let you know: The zip file for downloading the files is corrupt.

Thanks again,

Mr. H.
Thanks for notifying me about the zip archive, I've fixed the attachment.

Cheers,