Entry
How can I send mail notifying the webmaster from a 404 Not Found error page?
What environment variable holds the missing url when Apache redirects to the 404 page?
Jul 1st, 1999 22:54
Nathan Wallace, Chris Lott, Peter, Ron Smith
When Apache cannot find the page specified by the URL it redirects the
request to the error document as specified in the httpd.conf file. When
this happens the environment variable REQUEST_URI (accessible as
$REDIRECT_URI in PHP) holds the requested (but not found) URL.
Be careful though, because if the error document directive in the
httpd.conf points to an http:// doc, the redirect variables are lost.
Use a local file ref and everything should work fine, returning redirect
environment variables as expected.
Sending mail to the webmaster notifying them of the error is simple if
you just specify your error document to be a PHP script. To report the
URL that is missing just include a reference to $REDIRECT_URI in the
message.
Sending mail on every 404 request can end up placing a fair burden on
the inbox of the webmaster. An effective work around is to create a
page where the user can just click a button to send a message to the web
master reporting the error. That way, requests for missing images and
so on will not be reported. Here is the code to return the button and
mail when clicked:
<script language="php">
if (eregi("(form)",$state)) {
echo "<font size=\"5\">Thank You!</font>\n";
mail(
"webmaster@isp.com",
"Busted Link!",
"Link: $referer\n Using: $useragent\n IP:$ip\n Who: $user");
}
else {
echo "<form method=\"post\" action=\"notfound.phtml\">\n";
echo "<input type=hidden name=state value=form>\n";
echo "<input type=hidden name=referer value=\"$HTTP_REFERER\">\n";
echo "<input type=hidden name=useragent
value=\"$HTTP_USER_AGENT\">\n";
echo "<input type=hidden name=ip value=\"$REMOTE_ADDR\">\n";
echo "<input type=submit value=\"Click here to notify the Webmaster
about the missing page\">\n";
echo "</form>\n";
}
</script>