views:

24

answers:

2

Let's say that I want some generic error handling, which can be called from all over...

Ideally, I would like to just readirect to an error page using header('Location:... but, of course I can't do that it if any output has been written, so ...

1) how can I detect if any output has been written?
2) how can I load a new page if it has?

Perhaps I should buffer the entire page and only output it once I am certain that it is complete and no more errors can occur?

+1  A: 

Use PHP's built-in output buffering. At it's simplest, all you need to do is call ob_start() before anything that would produce output.

There are other options you can take advantage of as well if you desire:

http://www.php.net/manual/en/function.ob-start.php

Amber
+1 Yes, that's what I was thinking; I just wanted to confirm. Thanks.
LeonixSolutions
+2  A: 

1) how can I detect if any output has been written?

use headers_sent()

2) how can I load a new page if it has?

by echo'ing out a meta or a javascript redirect

note: probably nicer to use some output buffering, or even better don't echo anything until all your processing is done

nathan