Briefly: The following PHP script / mini tutorial will help you 'manage' http errors such as error 404 and some others easily. As the webmaster, you will receive a notification email for every occurence of the following errors off your website if you follow the instructions in detail:
- Error 400
- Error 401
- Error 403
- Error 404
- Error 500
n.b. As long as you edit the values in the example .htaccess file below, you may define exactly which error codes (off the list above) you want to 'control / customise' via this script.
First, we have to assume a few things so you, the reader, may find it a bit easier to 'modify' the scripts below to suit your own settings...
"The Example Website" information:
- server account username : example
- domain name : example.com
- path to the home page : /home/example/public_html/index.html
- path to error file : /home/example/public_html/error.php
- path to functions file : /home/example/public_html/functions_err.inc.php
This script requires you to upload 2 new PHP files and modify or upload one
.htaccess file.
1. .htaccess
The
.htaccess file is a hidden file. So, please backup this file if it already exists on your WWW folder, i.e.
/home/example/public_html/.htaccess. Like I said, it's a hidden file, so if you can't see this file or don't know how to 'view' hidden files off your web server, please consult your web host.
Create / Edit the
.htaccess file and insert the following lines:
Code:
ErrorDocument 400 /error.php
ErrorDocument 401 /error.php
ErrorDocument 403 /error.php
ErrorDocument 404 /error.php
ErrorDocument 500 /error.php
2. functions_err.inc.php
Copy the following code and file name it:
functions_err.inc.php. Remember to fill in the details inside this file where it says "FILL IN THE FOLLOWING DETAILS" :p
PHP Code:
<?php
/**
* @domain: DESILVA.BIZ
* @file: FUNCTIONS_ERR.INC.PHP
* @author: J de Silva
* @website: www.desilva.biz
* @email: scripts[AT]desilva[DOT]biz
* @copyright: Gen.I designs
* @date: November 16th, 2003
* @version: 1.0
* @about: The 'engine' behind a simple Error 404 handler.
*
/*===================================*/
/////////////////////////////////////////////////////////////////////
// FILL IN THE FOLLOWING DETAILS
/////////////////////////////////////////////////////////////////////
/* Set the email address that will receive error notifications */
$to = 'admin@example.com';
/* Set your domain name below, without the 'http://' or 'www' bits */
$domain = 'example.com';
/* That's ALL, nothing more to edit below.... */
#-------------------------------------------------------------------#
/////////////////////////////////////////////////////////////////////
// DEFINE SOME REQUIRED CONSTANTS...
/////////////////////////////////////////////////////////////////////
define( 'ADMIN_EMAIL', $to );
define( 'ADMIN_DOMAIN', $domain );
define( 'NL', "\r\n" );
unset( $to, $domain );
#-------------------------------------------------------------------#
/////////////////////////////////////////////////////////////////////
// Required functions
/////////////////////////////////////////////////////////////////////
function load_request_uri()
{
$qs = ( isset($_SERVER['REDIRECT_QUERY_STRING']) ? '?'.$_SERVER['REDIRECT_QUERY_STRING'] : '' );
return( $_SERVER['REDIRECT_URL'].$qs );
}
function is_reported()
{
if( isset($_COOKIE['http_errors']) )
{
$_COOKIE['http_errors'] = unserialize( ($_COOKIE['http_errors']) );
if( is_array($_COOKIE['http_errors']) )
{
// I cannot recall why now, but I remember noticing that
// $_SERVER['REQUEST_URI'] was NOT always available...
if( !isset($_SERVER['REQUEST_URI']) )
{
$_SERVER['REQUEST_URI'] = load_request_uri();
}
if( in_array($_SERVER['REQUEST_URI'], $_COOKIE['http_errors']) )
{
// this error page / url has been reported by this person before
return( TRUE );
}
}
}
// this person has either NEVER reported an error before
// or this is a NEW url error to report
$_COOKIE['http_errors'][] = $_SERVER['REQUEST_URI'];
$value = serialize( $_COOKIE['http_errors'] );
setcookie( 'http_errors', $value, time() + 24*60*60, '/', '.'.ADMIN_DOMAIN, 0 );
unset( $value );
return( FALSE );
}
function send_error_email( $error_code )
{
if( !is_reported() )
{
if( !isset($_SERVER['REQUEST_URI']) )
{
$_SERVER['REQUEST_URI'] = load_request_uri();
}
// the referring page, if any
$referred_by = ( isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'Unknown' );
$subject = "An error has occured - type: $error_code";
$message = 'The following error has occured:'.NL
.'--------------------------------'.NL.NL
." Type : $error_code".NL
." Page : {$_SERVER['SERVER_NAME']}{$_SERVER['REQUEST_URI']}".NL
." Referred from : $referred_by".NL
.' Time : '.date('d/m/Y H:i:s').NL
." From IP : {$_SERVER['REMOTE_ADDR']}".NL.NL
.'Regards,'.NL
.'Your hard-working web server.';
$headers = "From: webserver@{$_SERVER['SERVER_NAME']}\n"
."Reply-To: webserver@{$_SERVER['SERVER_NAME']}\n"
."X-Mailer: PHP/".phpversion();
// send the email
mail( ADMIN_EMAIL, $subject, $message, $headers );
unset( $error_code, $referred_by, $subject, $message, $headers );
}
}
#-------------------------------------------------------------------#
?>
3. error.php
This is the web page that most of your readers will see once the encounter an error on your website. You just need to fill in the HTML bits towards the end of the page where it says so.
PHP Code:
<?php
/**
* @domain: DESILVA.BIZ
* @file: ERROR.PHP
* @author: J de Silva
* @website: www.desilva.biz
* @email: scripts[AT]desilva[DOT]biz
* @copyright: Gen.I designs
* @date: November 16th, 2003
* @version: n/a
* @about: Error Notification Page.
*
/*===================================*/
include_once( './functions_err.inc.php' );
$error_code = ( !isset($_SERVER['REDIRECT_STATUS']) ? 'Undefined' : intval($_SERVER['REDIRECT_STATUS']) );
send_error_email( $error_code );
?>
<html>
<head>
<title>HTTP ERROR: <?php echo $error_code; ?></title>
</head>
<body>
<!--
START, INSERT YOUR HTML / WEB PAGE HERE.
NOTE THAT INTERNET EXPLORER DOES NOT LOAD
YOUR CUSTOM ERROR PAGE IF THE FILE SIZE
OF THIS WEB PAGE IS SMALL, I WILL TRY
TO LOOK FOR A REFERENCE ABOUT THIS SOON...
-->
</body>
</html>
Upload all 3 files to the WWW root folder on your web server, in other words:
/home/example/public_html