Display Custom Error Message in PHP

Introduction

In this article I explain displaying custom error messages in PHP. I have just created a simple custom error page. The custom error page that will handle the most common errors, such as errors 400, 401, 403, 400 and 500 using the Query string information in a switch() case statement.
First of all you need to change the "httpd.conf" file in Apache. Such as shown in the following.

Unchanged apache conf file:

Display custom error2.jpg

Changed apache conf file:

Display custom error1.jpg
Example

<html>

<head><title>Custom error page</title></head>

<body>

<?php

switch($_SERVER['QUERY_STRING'])

{

case 400:

echo '<h2>bad request</h2>';

echo '<h2>Error code 400</h2>';

echo '<p>the browser has made a bad request</p>';

break;

 

case 401:

echo '<h2>Authorization Required</h2>';

echo '<h2>Error code 401</h2>';

echo '<p>You have supplied the wrong information to access a secure resources</p>';

break;

 

case 403:

echo '<h2>Access Forbidden</h2>';

echo '<h2>Error code 403</h2>';

echo '<p>You have been denied access to this resource</p>';

break;

 

case 404:

echo '<h2>Page Not Found</h2>';

echo '<h2>Error code 404</h2>';

echo '<p>The page you are looking for not found</p>';

break;

 

case 500:

echo '<h2>Internal Server Error</h2>';

echo '<h2>Error code 500</h2>';

echo '<p>The server has encountered an internal error</p>';

break;
 

default:

echo 'Error Page';

echo '<p>This is custom error page..</p>';

echo '<p><a href="mailto:[email protected]">contact</a>The system administration if you feel this to be in error</p>'

?>

</body>

</html>
 

Output

Display custom error.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all