Sr. Web Developer
mediabistro.com
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume

Using PHP Error Handling
Throwing errors from PHP pages
Throwing errors from a PHP page is quite the same as emulating Apache's behaviour for ErrorDocument directives, you simply redirect the user using a query-string that specifies variables that Apache usually sets as environment variables. This makes it possible to use the same error page for all kinds of errors. An example:

<?php

function throw_error($message) {
  
$error_page = "/err/error.php";

  
$error_url = $error_page;
  
$error_url .= "?REDIRECT_ERROR_NOTES=$message";
  
$error_url .= "&REDIRECT_URL=" . $GLOBALS["PHP_SELF"];
  
$error_url .= "&REDIRECT_REQUEST_METHOD=$REQUEST_METHOD";
  
$error_url .= "&REDIRECT_STATUS=501";
  
Header("Status: 501");
  
Header("Location: $error_url");
  exit;
}

ob_start();  // Use output buffering to be able to throw errors anywhere on this page.

if(!condition) {
    
throw_error("the condition failed");
}

ob_end_flush(); // Page rendered, flush the output buffer.

?>
Using the PHP4 feature called output buffering also helps creating generic error reporting functionality. By not flushing the output buffer until you are sure the whole page has rendered error-free, you are able to make Header calls anywhere in your code to redirect the user.
I'll leave up to the reader to design and implement his/her own error.php page to suit his/her site. Don't forget that you can include a form with email submission possibilities for the users to send you comments.

[Page 1]  [Page 2]  


Comments:
Retrieve Javascript value in same php filesaravanan10/19/05 22:35
ereg functionBalaji09/11/05 10:29
PLEASE HELPkenneth07/26/05 22:35
php context menusugeeth03/07/05 00:45
RE: Server ErrorMAGnUm06/25/03 15:12
Not the best way to handle errorsKeith Ganger11/25/02 12:57
RE: Update Database ORACLEyongyut10/13/02 07:41
0 for $REDIRECT_URL for every page!coda10/02/02 05:06
php chat scriptsjaryd09/30/02 07:38
Apache subdomain helpJoseph09/19/02 11:52
php in netscape ??Jean Pierre09/02/02 15:45
PHP/PostgreSQL file upload/open script mike08/16/02 06:50
a PHP file?Dave the warrior06/12/02 07:39
RE: Java Script and PHPderek05/30/02 00:47
php file exist check on serverAnushree05/23/02 02:24
Error handling when page not foundAnushree05/22/02 10:01
TNS: no listenermaurice05/20/02 22:22
update oracle database , how?Melissa04/18/02 14:13
RE: PHP File Upload-please help meDrDre04/10/02 07:21
session function(php4)calistan thaya04/07/02 17:39
questionrdm03/11/02 23:42
Server ErrorArun02/27/02 07:05
RE: Forcing IE5 to accept your error pageYuzree Esmera02/10/02 08:08
REQUEST)URI and REDIRECT_URL are empty ?!?Alexandre Danault02/09/02 16:21
.php Steven12/14/01 22:11
RE: Handle ExceptionsSaravanan 12/12/01 00:39
RE: subdomain error handlingCraig12/10/01 11:32
RE: REDIRECT_URL pointed to processing scriptAdes11/21/01 19:25
RE: can i download an error script anywhere?Mark Fleming10/23/01 13:40
RE: PHP File Upload-please help meSaravanan 10/04/01 01:40
Update Database ORACLEFelix10/03/01 02:45
Java Script and PHPFelix10/03/01 02:44
PHP File Upload-please help meSaravanan 10/03/01 01:18
RE: Forcing IE5 to accept your error pageEric09/20/01 04:10
RE: server excludedJoffrey van Wageningen09/13/01 06:00
server excludedPeter van Beelen09/08/01 08:43
RE: REDIRECT_URL pointed to processing scriptSarah08/31/01 01:10
REDIRECT_URL pointed to processing scriptAngCK08/09/01 01:06
premature script header errorsatish07/21/01 05:05
RE: subdomain error handlingMorten Gade Sørensen07/17/01 10:25
RE: subdomain error handlingMarco DT07/16/01 09:13
RE: subdomain error handlingJade07/05/01 20:44
RE: subdomain error handlingAdam Miller07/05/01 15:34
can i download an error script anywhere?XTJ706/29/01 07:43
subdomain error handlingNathan Burke06/12/01 05:40
RE: Forcing IE5 to accept your error page*KWUK*05/29/01 00:00
Great!!!!!!!!Sarah05/27/01 09:52
Handle ExceptionsJailton Oliveira05/07/01 15:22
RE: Forcing IE5 to accept your error pagefryguy03/10/01 21:09
Forcing IE5 to accept your error pageGeoff Caplan11/13/00 13:55
 

If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.