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
https://forums.phpfreaks.com/topic/54060-output-buffering/#findComment-267339
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.