Home arrow Apache arrow Page 3 - Trap And Get Notified: A Practical Solution To 404 Errors With PHP
APACHE

Trap And Get Notified: A Practical Solution To 404 Errors With PHP


Tired of seeing the same 404 errors in your log files? In this article Matt uses .htaccess and PHP to combat nasty 404 errors and get notified about them immediately as they occur.

Author Info:
By:
Rating: 4 stars4 stars4 stars4 stars4 stars / 7
August 13, 2002
TABLE OF CONTENTS:
  1. · Trap And Get Notified: A Practical Solution To 404 Errors With PHP
  2. · Creating the .htaccess file
  3. · Creating the PHP Error Document
  4. · Uploading The Files
  5. · Conclusion

print this article
SEARCH DEVARTICLES

Trap And Get Notified: A Practical Solution To 404 Errors With PHP - Creating the PHP Error Document
(Page 3 of 5 )

Again, using your favourite web page editor, create a new file and save it as 404.php. This will be the document called and displayed by the .htaccess file when a 404 error occurs.

Firstly, we need to include the configuration file that we've just created, so add the following text:

<?php

//include the config file...
include "config.php";

?>


It would be handy to see what the user typed in to the URL that actually called the error page, so we use some environment variables to get this information. $HTTP_HOST will get the host name (eg: www.yourdomain.com) and $REQUEST_URI will actually get whatever comes after the host name in the URL (eg: /somefolder/somepage_not_exist.html).

To add these, we store them as a string and concatenate "http://" to the string to create a whole link. Add the following to your code, under the code we wrote above:

// get the URL that has resulted in the error being called
$where = "http://$HTTP_HOST$REQUEST_URI";


Next we are going to create the code that will actually send the email once the page is called. Add the following code under the part you added above:

//set the mail message up
$to= $mailto; //$mailto is defined in config.php
$subject= "Error 404 fuond";
$mailheaders= "From: $from"; // from is defined in config.php
$body .= "A 404 Error was found on your website: $url\n\n\n";
$body .= "The page requested was: $where\n\n";

// now send the email
mail($to, $subject, $body, $mailheaders);


That completes all of the PHP code. Your 404.php script should look like this:

<?php

//include the config file...
include "config.php";

// get the URL that has resulted in the error being called
$where = "http://$HTTP_HOST$REQUEST_URI";

//set the mail message up
$to= $mailto; //$mailto is defined in config.php
$subject= "Error 404 fuond";
$mailheaders= "From: $from"; // from is defined in config.php
$body .= "An ERROR 404 was found on your website: $url\n\n\n";
$body .= "The page requested was: $where\n\n";

// now send the email
mail($to, $subject, $body, $mailheaders);

?>


We're now going to add the HTML output to the screen, which will inform the user of the error. Add this code after the block of PHP code in 404.php:

<html>
<head>
<title>ERROR 404 - Page Not Found</title>
</head>
<body>
<table width="80%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td><p>&nbsp;</p>
<h2><font color="#990000">Error 404</font> Page Not Found</h2>
<p>Sorry, the page you requested does not exist, or has been moved. <p>
<p>Please click the link below and return to the homepage<p>
<p><a href="<? echo "$url"; ?>">Click here to return to the homepage</a></p>
</td>
</tr>
</table>
</body>
</html>


You will notice that the URL you specified in the config.php file is used above in the HTML code. The <? Echo "$URL"; ?> code simply gets the URL value from the configuration file and makes the link. This HTML code is simple, but you could add your own company logo or include your corporate colours to make the page look like all of the other pages on your site.

Save the 404.php file. Next, we're going to upload the files we created.
blog comments powered by Disqus
APACHE ARTICLES

- Programmatically Manipulating Microsoft Exce...
- Installing PHP under Windows
- Compressing Web Content with mod_gzip and mo...
- Compressing Web Output Using mod_deflate and...
- Setting Up Apache 2.0.45 to Parse PHP Pages
- Custom Error 404 Documents with PHP
- Using Apache and PHP on Mac OS X
- ASP: Active Sessions, Active Logins and Tota...
- Working With Oracle on Windows: Part 1
- The Quick-n-Dirty Guide to Setting Up Apache...
- Installing Apache With SSL: The Complete Gui...
- 7 Powerful .htaccess Customization Tips
- Trap And Get Notified: A Practical Solution ...
- One Way To Use Server Side Includes
- Using ForceType For Nicer Page URLs

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials