chriscloyd Posted June 3, 2007 Share Posted June 3, 2007 what does output buffering do? Quote Link to comment Share on other sites More sharing options...
chronister Posted June 3, 2007 Share Posted June 3, 2007 Output buffering does pretty much what it states, All output (stuff displayed on screen) is stored in a buffer first. When the entire script is run it will flush the buffer and display the stuff on screen. It helps clear up "Cannot Modify Headers ....... " type errors. For instance. <html> <head><title></title></head <body> Welcome to my site. <?php if(!isset($user_login)) { header('Location:login.php'); }else { echo 'Your Logged In'; } ?> </body> </html> With this chunk of code, anytime the if statement returned true ($user_login is not set) the header would try to redirect, and cause a fatal error. The script would not run past that point because of the html above it. If you HAVE to have a header redirect in the body of the page, you can circumvent the error with output buffering e.g. <?php ob_start() ?> <html> <head><title></title></head <body> Welcome to my site. <?php if(!isset($user_login)) { header('Location:login.php'); }else { echo 'Your Logged In'; } ?> </body> </html> <?php ob_end_flush() ?> This would store the entire chunk of code in buffer (memory) until the whole thing was finished and then it would flush (display on screen ) the results of the code above. Check out the manual for all output control functions. http://us2.php.net/manual/en/ref.outcontrol.php I hope this helps. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.