PHP Classes

File: custom-error-howto.0.13.txt

Recommend this page to a friend!
  Classes of Alex Kemp  >  Conteg Content Negotiation  >  custom-error-howto.0.13.txt  >  Download  
File: custom-error-howto.0.13.txt
Role: Documentation
Content type: text/plain
Description: Info + brief How-To on creating custom error pages, including coding examples.
Class: Conteg Content Negotiation
Negotiate HTTP Request Response
Author: By
Last change: Added references
Date: 14 years ago
Size: 4,434 bytes


Class file image Download
Conteg v0.13 - Content Negotiation + Cache-Control for PHP-originated Web Output.

Custom Error Pages

With the appropriate switches, Conteg will auto-send the correct:

	* 304 Not Modified
	* 406 Not Acceptable
	* 412 Precondition Failed
	* 416 Requested Range Not Satisfiable
	* 206 Partial content, or
	* 200 OK page
	+ full headers

A website may also want to take account of other error situations, requiring:

	* 404 Not Found             : content that no longer exists
	* 410 Gone                  : ditto, HTTP/1.1 only
	* 403 Forbidden             : content that is refused
	* 500 Internal Server Error : generic Server Error
	* 503 Service Unavailable   : service refused, server unavailable
	  (and so on - see sendStatusHeader() for a full list)

This short help file is to give a brief introduction to the production of such error pages,
including some pseudo-PHP to indicate typical coding.

Custom 404 pages were introduced in Conteg at v0.12.1, and are extended with v0.13. It is now
possible to:

	* declare any arbitrary HTTP Status (no checking!)
	* declare `404 Not Found'
	  - by default, transformed to `410 Gone' when HTTP/1.1
	  - will over-ride any other HTTP Status

By default, all (MS-only affected) error pages are auto-fixed to avoid the infamous
MSIE `friendly` error pages.

As with all other parameters, all setup() defaults may be over-ridden.

These are the relevant instantiation/setup() parameter defaults:

$_Instance = new Conteg( array(
   '404'            => FALSE, // higher precedence than 'http_status'
   '404_to_410'     => TRUE,  // see sendStatusHeader()
   'http_status'    => NULL,  // preferred to program-decided status
   'msie_error_fix' => TRUE,  // avoid MSIE `friendly` error pages
   'noprint'        => FALSE

Implementation Overview

Because this is a non-simple usage of Conteg, it will need to be declared early (possibly within
an auto-prepend file) with the `'noprint' => TRUE' switch. Then, at any point within the program
flow at which an error has been caught, setup() and show() are used, and the program exited.

The following pseudo-PHP will assume that an $error array is used to store Status and output.

	ob_start();                  // Conteg requirement
	error_reporting( E_ALL );    // this is a test file
	include( 'Conteg.include' );

	$error     = array();        // to store and signal error info
	$param     = array(
	   'noprint' => FALSE
	$_Instance = new Conteg( $param );

	(normal program flow)

	// error check
	if( <critical-error-check> ) {
		$error[ 'critical' ] = TRUE;
		$error[ 'status' ]   = 403;
		$error[ 'text' ]     = "<html><body><p><b>Buzz Off</b><br />\n".
		                       'A bit of text to explain why.</p></body></html>';

	(other program flow until ready to check all errors)

	// bail out on critical errors
	if( !empty( $error[ 'critical' ])) {
		ob_end_clean();           // discard previous output
		echo $error[ 'text' ];
		$param = array(
		   'cache_control' => array( 'macro' => 'cache-none' ),
		   'http_status'   => $error[ 'status' ]
		$_Instance->setup( $param );
		// any prog cleanup or processing goes here
		// careful not to cause any more output

	(normal program flow)

Some references

	*    MSIE `friendly' Error Pages:
	*                               :

	*    HTTP/1.1:                  :
	*    HTTP/1.0:                  :

	*    v0.13 release announcement :
	*    v0.12.3                    :
	*    v0.12.2                    :
	*    v0.12.1                    :
	*    v0.10                      :

(c) Alex Kemp 28 February, 2007
(email address with-held due to historical/hysterical problems)
(contact instead via PM at
For more information send a message to info at phpclasses dot org.