Apache
  Home arrow Apache arrow Page 3 - Trap And Get Notified: A Practical Solutio...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
APACHE

Trap And Get Notified: A Practical Solution To 404 Errors With PHP
By:
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 3
    2002-08-13

    Table of Contents:
  • Trap And Get Notified: A Practical Solution To 404 Errors With PHP
  • Creating the .htaccess file
  • Creating the PHP Error Document
  • Uploading The Files
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More Apache Articles
    More By


     

    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







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek