Papermashup

Subscribe


Tweets


"RT @kycutwilson: @ashleyford @burgerbeartom incredible. There's 5 more left! Shout about it!!"

@ashleyford 2 days ago

"@burgerbeartom @kycutwilson finally got around to using my free burgers for life! And we weren't disappointed. http://t.co/nJ6p2nLtg4"

@ashleyford 1 week ago

Designer and web developer, Co-founder and Technical Director at Harkable.com. Previously I worked at Spotify, MySpace and InMobi. Contact me - ashley[at]papermashup.com

Papermashup

Create an error page to handle all errors with PHP

Use PHP to handle what the user sees when a redirect header status is detected

AshleyAshley

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

The 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.



$status = $_SERVER['REDIRECT_STATUS'];
$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>
<p>'.$message.'</p>';

Designer and web developer, Co-founder and Technical Director at Harkable.com. Previously I worked at Spotify, MySpace and InMobi. Contact me - ashley[at]papermashup.com

Comments 11
  • Thomas
    Posted on

    Thomas Thomas

    Reply Author

    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”);
    include(‘errors.php’);
    exit;
    }
    else{
    //continue along….
    }


    • Thomas
      Posted on

      Thomas Thomas

      Reply Author

      or with

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

      rather


  • Billy
    Posted on

    Billy Billy

    Reply Author

    Thanks are lot for the code 😀


  • Elvis
    Posted on

    Elvis Elvis

    Reply Author

    It did not work for me!


    • sivA
      Posted on

      sivA sivA

      Reply Author

      Me Too —–


  • Everio
    Posted on

    Everio Everio

    Reply Author

    practical and useful :p


  • Jonathan Dusza
    Posted on

    Jonathan Dusza Jonathan Dusza

    Reply Author

    Very useful script. Viva la PHP


  • Maicon Gabriel Schmitz
    Posted on

    Maicon Gabriel Schmitz Maicon Gabriel Schmitz

    Reply Author

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


    • Maicon Gabriel Schmitz
      Posted on

      Maicon Gabriel Schmitz Maicon Gabriel Schmitz

      Reply Author

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


    • Ashley
      Posted on

      Ashley Ashley

      Reply Author

      Thanks for spotting. all fixed