This Custom 404 PHP script will display an error page and send you an email with all the relevant information when there is a 404 error. It is useful for notifying you of bad links to your site and it is also a great debugging tool, notifying you of your own invalid references. You may want to turn on the email feature selectively (for testing your site and/or periodic checks) rather than leave it on all the time as it could generate a lot of emails if someone keeps making bad requests.
In order to use it you must add the following line to your .htaccess file. DO NOT use the full URL to the error page, use the relative path as done in the example below.
ErrorDocument 404 /404.php
Assuming the script below is named 404.php and placed in your htdocs directory.
- - Start Script Here - -
- - End Script Here - -
In order to use it you must add the following line to your .htaccess file. DO NOT use the full URL to the error page, use the relative path as done in the example below.
ErrorDocument 404 /404.php
Assuming the script below is named 404.php and placed in your htdocs directory.
- - Start Script Here - -
<?php
$sendMail = "yes"; // must be set to "yes" for the email to be sent$emailAddress = "you@yourdomain.com"; // set to your email addressputenv('TZ=EST5EDT'); // set to your time zone
// change nothing below this line$domain = $_SERVER['HTTP_HOST'];$page = $_SERVER['REQUEST_URI'];
if ($sendMail == "yes") {
$errortime = (date(" F d h:ia"));
$browser = $_SERVER['HTTP_USER_AGENT'];
$referer = $_SERVER['HTTP_REFERER'];
$IP = $_SERVER['REMOTE_ADDR'];
$message = "";
$message .= "Time of the error: $errortime\n\n";
$message .= "browser: $browser\n\n";
$message .= "Page Requested: $domain$page\n\n";
$message .= "Referer: $referer\n\n";
$message .= "IP Address: $IP\n\n";
$name = gethostbyaddr($IP);
$message .= "Hostname: $name\n\n";
mail($emailAddress, "404 Error" , $message, "From: Website <>");
}header("HTTP/1.0 404 Not Found");?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>404 - where oh where?</title>
<style type="text/css">
A:link {text-decoration: none; font-weight:bold; color: blue}
A:hover {color: red}
</style>
</head>
<body>
<div style="width: 400px; position: absolute; top: 45px; left: 50%; margin-left: -200px;">
<span style="color: teal; font-size: 32px;">file not found</span>
<br /><br /><br />
<span style="color: black; font-size: 18px;">
(oops)
<br /><br />
it appears you were looking for
<br />
<span style="color: teal; font-size: 18px;"><?=$domain?><?=$page?></span>
<br /><br />
however, since you are here, it is clear you did not get what you wanted.
<br /><br />
the problem has been reported so any broken links can be found and repaired.
<br /><br />
you can click <a href="javascript:history.go(-1)">here</a> to go back to your previous page.
</span>
</div>
</body>
</html>- - End Script Here - -
Comments
Post a Comment