scriptygoddess

18 Mar, 2004

404 Error Handling part II

Posted by: Jennifer In: Scripts

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.

15 Responses to "404 Error Handling part II"

1 | Kolby K.

March 19th, 2004 at 11:24 am

Avatar

My 404 page code and all other error page codes are setup in their own root directory, error pages. This root directy is top level above all http, cgi, and all other documents. What would I do in this case since I use no htaccess file?

2 | Jennifer

March 19th, 2004 at 3:38 pm

Avatar

Thank you so much for posting this! Now, rather than being overwhelmed by 404s, I can just fix them when I receive emails. It's wonderful. :)

3 | Jennifer

March 19th, 2004 at 3:52 pm

Avatar

Kolby K – your best bet is to talk to your host provider. They may even have an FAQ type thing to explain how to set up customized 404 pages…

4 | Kolby K.

March 20th, 2004 at 12:19 am

Avatar

I can customize my error pages. Basically I just create a html document with the correct filename for whatever error page I am dealing with and upload it into the proper root directory. It's just like uploading html pages into a regular web accessible folder.

I guess what I am really wondering is since you are dealing with an htaccess file on that script, will the script be effected since I dont use an htaccess file to direct my 404 pages, but instead just have a simple root directory in which I upload my error documents too. Maybe the htaccess file is not being used in a way that would effect the script.

Maybe all I need to do is just add that script into my html for my error pages and go on like I have been doing.

I am most likely making it more complicated then it is. :p

5 | Jennifer

March 20th, 2004 at 7:40 am

Avatar

Yes – you can just add it to your existing 404 page (without having to deal with the .htaccess stuff. That was just *extra* information not specifically relating to the script) – however, if your 404 is .html you may need to do something else (either changing the extension to .php or some other setting on the server) so the server will process the php code on that page.

6 | Murilo

April 1st, 2004 at 10:56 pm

Avatar

Hi,

I've been searching for a PHP version of this script

http://www.hotscripts.com/Detailed/3160.html

for ages now. Can you guys help me maybe?

As the original page seems to be down, I could make it avaiable to someone willing to port it from Perl.

7 | shmuel

April 29th, 2004 at 9:26 pm

Avatar

/me is confused. I put your script up on my site. It seems to work just fine but no matter what I request from my server (i.e. shmuel.org/blahblah ) the script only ever sees this as shmuel.org/missing.php – the file that contains this script and my 404 error. Any ideas?

8 | ladysilver

May 3rd, 2004 at 11:53 am

Avatar

shmuel, the problem is probably in your .htaccess path. You have to careful how you setup your script redirect. For example, if you set up an URL path like ErrorDocument 404 http://www.mysite.com/errordocs/404error.php then .htaccess is going to redirect as if it's going to a different website and you will get the script path in your email instead of the error path. Try putting your error documents in your root folder and write .htaccess as ErrorDocument 404 /404error.php. See if that fixes it.

9 | shmuel

May 3rd, 2004 at 1:24 pm

Avatar

Ladysilver,

Thank you. That's exactly what the problem was and adjusting my path in the .htaccess file made everything "right as rain". Thanks for your help.

10 | shmuel

May 4th, 2004 at 3:15 pm

Avatar

Ok, here we go again. I've got the script working on the server – it's emailing me correct URLs and correct referrers as far as I can tell. The only problem is once I reach 8 individual 404 errors in my missing.txt file the script starts repeating 008 as the currentKeyNum over and over again. This results in my receiving emails for files that I've already been email about. Any thoughts?

11 | scrapblog

March 19th, 2004 at 5:34 pm

Avatar

Very very clever 404
404 Error Handling part II…

12 | atog

May 29th, 2004 at 8:10 am

Avatar

404 script
Holy Cow! Yesterday I made a custom 404 page using this script. The scripts sends you a mail each time a person hits a 404 page. It also keeps track of previous missing files so you only get a mail once for each new request.

So here's where the Hol…

13 | noscope | Photography and 404 Updates

September 28th, 2004 at 8:10 pm

Avatar

[…] al parts custom PHP (oh yes, I'm learning!) and help from WeblogToolsCollection, and Scriptygoddess. Just follow this link that never were, to see how t […]

14 | Dasme

December 15th, 2004 at 11:25 pm

Avatar

Just dug out this old script again to look for missing files after a recent site rebuild, and I too am having the problem where the script will stop counting up at 008…

Any ideas on what is wrong? I've traced it down to teh php function parse_ini_file, but can't find any solutions to this problem on the net.

Anyone fix this little bug?

15 | Francois

March 17th, 2005 at 9:35 am

Avatar

I've added two lines to your 404 PHP script.

404 errors with no referer are not that useful. Here's a line that indicates in the subject of the email that there is no referer to this 404 alert:

if($HTTP_REFERER == "") $subject .= " [no referer]";

I also add a Google link search to every email: even where there is no referer, I can look for error sources.

$message .= "No referrer? Google this
link:\nhttp://www.google.com/search?q=link:$domain$REQUEST_URI\n\n

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements