Jump to content

output buffering


chriscloyd

Recommended Posts

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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.