tech-pro
06-27-2007, 01:10 PM
In PHP, is there a way to force the browser to open a new page after the script has partly output a page?
I'm trying to improve a script which tends to spray PHP warning messages over the page whenever the MySQL server stops responding. I have written an error handler that will output a message and close the session when this happens (okay, actually I found one on the web using Google) but it still doesn't look very nice if the script has started to output some of the page when the error occurs.
I'd like to force a new page so my error message comes up on a clean page.
Rewriting the script so nothing is sent to the browser until the whole page has been completed OK isn't an option.
Crucial Web Host
06-27-2007, 01:16 PM
You should just be able to run the script, if it errors set error = 1 and then just set an if error = 1 then write your header redirect out.
This requires that all script processing is done prior to wring the page, though.
Rick
(re-reading your question - there really is not a way to do it with PHP after the output has already began. You could probably come up with some kind of javascript solution, but I know nothing about that.)
Maybe this will be of assistance?
Beside ReWrite your script so that no output before header(), there is a dirty workaround — turn on output_buffering, a PHP setting in php.ini. Set output_buffering to On will allow you to send header lines even after you send body content, at the price of slowing PHP’s output layer a bit.(quoted from php.ini)
However, if your site are on shared hosting environment then you probably no access to the php.ini file to change the setting…
No problem, we can use .htaccess!
You can find the rest of the article here -> http://www.liewcf.com/blog/archives/2004/07/get-rid-php-header-redirect-error/
Engelmacher
06-27-2007, 01:54 PM
Rewriting the script so nothing is sent to the browser until the whole page has been completed OK isn't an option.
Then there aren't any options apart from fixing your mySQL server so it doesn't go down all the time.
sasha
06-27-2007, 02:11 PM
You could try and connect to the database before you send any output and if that fails do not bother with the rest of the page.
Or uglier, you could catch mysql errors as the code goes along and do something like this:
if (!$result) {
/* mysql server is down again, this is quick
fix by the time i find better hosting service */
jsRedirect();
}
function jsRedirect(){
$url = 'http://some_url' ;
echo <<<END
There was an oops! <a href="$url">go here</a>
<script type="text/javascript">
window.location = '$url' ;
</script>
END;
exit;
}
tech-pro
06-28-2007, 03:51 AM
Thanks for the suggestions.
I don't really know what causes these errors. The code starts with the usual "connect or die." The connection to the DB does not fail, but any attempt to retrieve a row fails, which then produces errors whenever that result is used. Wait a couple of minutes and it all works fine.
Engelmacher
06-28-2007, 09:51 AM
Then either your code or your server sucks (or both). Is there any legitimate reason why you cannot hold off on output until you know your queries have run successfully? I can't think of one.
avocado
06-28-2007, 01:23 PM
Why not use output buffering to prevent partial rendering and only output content and error messages after the whole thing has been output successfully?
Jatinder
06-28-2007, 04:52 PM
At top of your script add ob_start(); and then at the top of your error handler function call ob_end_clean();
All output will be buffered and then discarded whenever your error handler is called.
bimbiero
06-28-2007, 07:54 PM
I don't really know what causes these errors. The code starts with the usual "connect or die." The connection to the DB does not fail, but any attempt to retrieve a row fails, which then produces errors whenever that result is used. Wait a couple of minutes and it all works fine.
When retrieving your data from the database (mysql_query(statement). You could simply add an or die('Error - '.mysql_error()) or even create your function. I would maybe guess that you get the further errors cause your script is depending upon the results from the query?