ErrorDocument not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pritam79
Forum Commoner
Posts: 65
Joined: Wed Mar 26, 2008 9:28 am

ErrorDocument not working

Post by pritam79 »

Hi all,
I am trying this example forthe ErrorDocument directive. Firstly, i have made the following changes to my “httpd.conf” file.
I have changed this code of the httpd.conf file

Code: Select all

#
# Customizable error responses come in three flavors:
# 1) plain text 2) local redirects 3) external redirects
#
# Some examples:
#ErrorDocument 500 “The server made a boo boo.”
#ErrorDocument 404 /missing.html
#ErrorDocument 404 “/cgi-bin/missing_handler.pl”
#ErrorDocument 402 http://www.example.com/subscription_info.html
#
To this

Code: Select all

#
# Customizable error responses come in three flavors:
# 1) plain text 2) local redirects 3) external redirects
#
# Some examples:
ErrorDocument 400 /error.php?400
ErrorDocument 401 /error.php?401
ErrorDocument 403 /error.php?403
ErrorDocument 404 /error.php?404
ErrorDocument 500 /error.php?500
After this, i have created a file “error.php”-

Code: Select all

<html>
 <head>
 <title>Beginning PHP6, Apache, MySQL Web Development Custom Error Page </title>
 </head>
 <body>
<?php
switch ($_SERVER['QUERY_STRING'])
 {
  case 400:
    echo '<h1> Bad Request </h1>';
    echo '<h2> Error Code 400 </h2>';
    echo '<p>The browser has made a Bad Request.</p>';
    break;
     
  case 401:
    echo '<h1> Authorization Required </h1>';
    echo '<h2> Error Code 401 </h2>';
    echo '<p>You have supplied the wrong information to access a secure resource.</p>';
    break;
  
  case 403:
    echo '<h1> Access Forbidden </h1>';
    echo '<h2> Error Code 403 </h2>';
    echo '<p>You have been denied access to this resource.</p>';
    break;
    
  case 404:
    echo '<h1> Page Not Found </h1>';
    echo '<h2> Error Code 404 </h2>';
    echo '<p>The page you are looking for cannot be found.</p>';
    break;
                    
  case 500:
    echo '<h1> Internal Server Error </h1>';
    echo '<h2> Error Code 500 </h2>';
    echo '<p>The server has encountered an internal error.</p>';
    break;
     
  default:
    echo '<h1> Error Page </h1>';
    echo '<p>This is a custom error page...</p>';
}
                   
echo '<p><a href="mailto:sysadmin@example.com">Contact</a>  the system administrator if you feel this to be in error.</p>';
?>
</body>
</html>
But when i open my browser and type http://localhost/nonexistent/page.html, (or any other page that doesn’t reside on my server), into the address bar I don’t get the Page Not Found message on the screen like the one below-

Code: Select all

Page Not Found
Error Code 404
The page you are looking for cannot be found
Contact the system administrator if you feel this to be in error
Instead i get this message-

Code: Select all

Error Page 
This is a custom error page...
Contact the system administrator if you feel this to be in error.
Why is this not working?
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: ErrorDocument not working

Post by McInfo »

Add this just before the switch statement.

Code: Select all

var_dump($_SERVER['QUERY_STRING']);
Post Reply