In this post I grabbed this bit of code so that I would be emailed whenever someone hit a 404 page on my site. However, I found out that my 404 page was visited quite often… usually for the same three files.
So I modified the script a bit – so that it would keep a list of missing files. If a NEW page was requested (that didn't exist) it would email me THEN (only once) – and add that new page to the list.
So here's the revised code.
<?
//portions of this script are originally from shat.net
//the rest was put together by jennifer of scriptygoddess.com
# Set $domain to your domain name (no www)
$domain = "yourdomain.com";
# Set $docroot to the URL of the directory which contains your
# .htaccess file. Don't include trailing slash.
$docroot = "http://www.yourdomain.com";
# Set $emailaddress to the email address of whoever should be
# notified of 404 errors. Don't escape the @ symbol.
$emailaddress = "yourname@yourdomain.com";
//upload a BLANK TEXT FILE to your server
//named "missing.txt"
//CHMOD 777 on this file
//enter the full server path* to this text file below
$missinglist = "/home/youraccount/public_html/missing.txt";
//What I mean by full server path:
//http://www.scriptygoddess.com/archives/004860.php
//——–all done for customizing script——
function send_email() {
//this function originally from this script:
//http://shat.net/php/404/404Handler.php.txt
# Copyright 2000-2002 shaun@shat.net under the GPL v2+
# Request access to the global variables we need
global $REQUEST_URI, $HTTP_REFERER, $emailaddress, $REMOTE_ADDR, $docroot;
# Build the $errortime variable to contain the date/time of the error.
$errortime = (date("d M Y h:m:s"));
# Create the body of the email message
$message .= "404 Error Report\n\nA 404 error was encountered by $REMOTE_ADDR";
$message .= " on $errortime.\n\n";
$message .= "The URI which generated the error is: \n$docroot$REQUEST_URI\n\n";
$message .= "The referring page was:\n$HTTP_REFERER\n\n";
# Send the mail message. This assumes mail() will work on your system!
$headers = "From: $emailaddress\nDate: $errortime -0600\n";
$subject = "404 Error: $docroot$REQUEST_URI";
mail($emailaddress, $subject, $message, $headers);
return;
}
$missinglistarray = parse_ini_file($missinglist);
//here's the part that opens your list of missing pages
//if you run into problems writing to the file
//you can uncomment the "echo" lines
//to see where you're having a problem
//most of this code came from here:
//http://us4.php.net/manual/en/function.fwrite.php
if (in_array($REQUEST_URI, $missinglistarray)) {
//do nothing – it's already in the list
} else {
//send an email with the info
send_email();
//now add new missing page to the list
$currentKeyNum = str_pad(count($missinglistarray), 3, "0", STR_PAD_LEFT);
$addmissing = $currentKeyNum." = \"".$REQUEST_URI."\";\n";
// Let's make sure the file exists and is writable first.
if (is_writable($missinglist)) {
if (!$handle = fopen($missinglist, 'a')) {
//echo "Cannot open file ($missinglist)";
exit;
}
if (fwrite($handle, $addmissing) === FALSE) {
//echo "Cannot write to file ($missinglist)";
exit;
}
//echo "Success, wrote ($addmissing) to file ($missinglist)";
fclose($handle);
} else {
//echo "The file $missinglist is not writable";
}
}
?>
<!– this is just a VERY!! basic 404 page below but you can make a custom one. Just paste the code above before the code of your design. There's nothing below this that that you absolutely need –>
<head><title>File Not Found</title></head><body><H1>File Not Found</h1>The requested URL was not found on this server.<p></BODY>
As noted above you need to upload a blank text file named "missing.txt" – chmod 777 – and indicate the full server path in the script.
As for the CONTENT of the 404 – that can be just about anything you want. You can change that completely. Just include everything between the php tags <? ?> above your 404 page design.
As to how to get your server to USE your newly constructed 404 page – see this page for details.