Use your left/right keys to browse tutorials
Create an error page to handle all errors with PHP

Create an error page to handle all errors with PHP

1 Star2 Stars3 Stars4 Stars5 Stars
Posted on January 31, 2012

Here’s a very simple solution to handling a variety of HTTP errors like 404, 500.. etc in one php file. All we need to do is create an array of error codes and match against them by picking up the global redirect status code using PHP. This means that we can use one page the handle multiple errors.

The Code

You’ll need to update your .htaccess file so when an error is detected the server knows how to handle the request. In our case we’re going to forward all the listed errors to our generic errors.php file.

ErrorDocument 400 /errors.php
ErrorDocument 403 /errors.php
ErrorDocument 404 /errors.php
ErrorDocument 405 /errors.php
ErrorDocument 408 /errors.php
ErrorDocument 500 /errors.php
ErrorDocument 502 /errors.php
ErrorDocument 504 /errors.php


This is the contents of the ‘errors.php’ file. You need to save this in the root directory of your web server, unless you modify the path in the .htaccess file above.

$codes = array(
       400 => array('400 Bad Request', 'The request cannot be fulfilled due to bad syntax.'),
       403 => array('403 Forbidden', 'The server has refused to fulfil your request.'),
       404 => array('404 Not Found', 'The page you requested was not found on this server.'),
       405 => array('405 Method Not Allowed', 'The method specified in the request is not allowed for the specified resource.'),
       408 => array('408 Request Timeout', 'Your browser failed to send a request in the time allowed by the server.'),
       500 => array('500 Internal Server Error', 'The request was unsuccessful due to an unexpected condition encountered by the server.'),
       502 => array('502 Bad Gateway', 'The server received an invalid response while trying to carry out the request.'),
       504 => array('504 Gateway Timeout', 'The upstream server failed to send a request in the time allowed by the server.'),

$title = $codes[$status][0];
$message = $codes[$status][1];
if ($title == false || strlen($status) != 3) {
       $message = 'Please supply a valid HTTP status code.';

echo '<h1>Hold up! '.$title.' detected</h1>

More tutorials from Papermashup
  • ftwqertert


  • Thomas

    I can get the page to 404 correctly when a page doesn’t exist.

    However, I’m trying to use this to 404 a member’s area if the visitors does not have a user cookie set.

    I’m using this…it does show the errors.php page, but it does NOT display a 404 (just the message “Please supply a valid HTTP status code.”). What am I doing wrong?

    if (!isset($_COOKIE[“user”])){
    header(“HTTP/1.1 404 Not Found”);
    //continue along….

    • Thomas

      or with

      header(“HTTP/1.0 404 Not Found”);


  • Billy

    Thanks are lot for the code :D

  • Pingback: BaseCode » Своя страница обработки ошибок

  • Elvis

    It did not work for me!

    • sivA

      Me Too —–

  • Everio

    practical and useful :p

  • Jonathan Dusza

    Very useful script. Viva la PHP

  • Maicon Gabriel Schmitz

    Hi, the last line is wrong!
    Please correct to: echo ”.$message.”;

    • Maicon Gabriel Schmitz

      The post has changed my string…
      Correct the echo command on the last line.

    • Ashley

      Thanks for spotting. all fixed