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> </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.
Next: Uploading The Files >>
More Apache Articles
More By