SCSI Hard Drives
Search
Advanced Search

Categories


Recently Viewed

Clear List
Pages



My Links
Web Directory Index
A human edited, comprehensive web directory list.
Link Exchange
DesignFirms Link Exchange


Output buffering in php- the complete guide(tutorial)

By : yankees26an
Rating : Average Rating : 9.00 From 1 Voter(s)


 

The main reason output buffering in PHP is not known by everyone might be that it's a little bit hard to understand what it's good for :-) Basically, with this functionallity you can catch all output before the user sees it. Why?  You might ask... The answer is because... you can modify it in certain ways for your need.
Stupid example for explaining, but never use this :)

Examples are the best way to understand things... so here goes a very simple and stupid example, just to get you to understand WHAT outputbuffering does, and then after this we will talk about what GOOD things it can be used for.

 


<?
<strong>ob_start</strong>("ourCallbackFunction");
echo 
"Hi I am a banana and I want to be an orange";
<
strong>ob_end_flush</strong>();
function 
outCallbackFunction($theBuffer)
{
   
$theBuffer str_replace("banana""skateboard"$theBuffer);
   return 
$theBuffer;
}
?>
 

This code has got some things that are important and that is the two functions that are marked bold: ob_start and on_end_flush. ob_start makes sure that PHP holds back all output without showing anything for the use (until the script is ended or the function ob_end_flush is called). The difference between this and how a script normally is working is that normally everything that is echo:ed is displayed for the used directly.


The next important thing is the parameter passed to ob_start. The parameter is telling PHP to call the function with that name when it's gonna flush everything and show it to the user. In this case the function replaces all words "banana" with the name word "skateboard". So the output will be "Hi I am a skateboard and I want to be an orange". (I warned you that the example would be stupid, but you get the idea now I hope :-)
Now to a more useful example/tips off output buffering

Things that need to go to the http header needs to be echoed first of all in the output. Examples on that is setting cookies, setting how caching is handled and redirecting with header("location blabla.php"); Also sessions needs to be started at the evry beginning of scripts when no output has been done.

Most of the time you are able to do things like this first in your file, but not always. Let's say for example that a user is logging in and in your login script you already have called the script that writes out the beginning of your html design. How will you handle that a session is started to set the user to be logged in? You can't, but with ob_start you can :-)  If you start your login.php with the row "ob_start". Further down you check if the username and password is correct agains the database and want to start the session. That is ok now, because no output has been passed to the user. Magic :-) Just set the session-data and you can also do a nice header("Location: loggedin.php"); exit; to pass the user to a page only for logged in users. If username was not correct you just echo that as usual and at the end of your script you call ob_end_flush and everything is displayed to the user.
Making your session parameter XHTML valid with output buffer

You might know that the letter & is not a valid XHTML character, and it should always be converted to &amp; When you start a session, in some configurations your urls will look like this <a xhref=index.php?action=view&PHPSESSID=33d5f200dffdf343224"> because that is a way of passing the PHPSESSION-id on to the next page. With this small callback you can make that url XHTML valid :-)

 

Pretty simple it takes the text going to the user and replaces &PHPSESSID with &amp;PHPSESSID. This way you have made your browser much more happy!








Comments / Feedback

RSS 2.0: Syndicate this article

Add Comment
* Name


* Email Address


Site



*Image Validation (?)


*Comments / Feedback





Print Article Print Article Send to a friend Send to a friend Bookmark Article Bookmark Article Save as PDF Save as PDF Social Bookmarking
Add to: Mr. Wong Add to: Webnews Add to: Icio Add to: Oneview Add to: Folkd Add to: Yigg Add to: Linkarena Add to: Digg Add to: Del.icio.us Add to: Reddit Add to: Simpy Add to: StumbleUpon Add to: Slashdot Add to: Netscape Add to: Furl Add to: Yahoo Add to: Spurl Add to: Google Add to: Blinklist Add to: Blogmarks Add to: Diigo Add to: Technorati Add to: Newsvine Add to: Blinkbits Add to: Ma.Gnolia Add to: Smarking Add to: Netvouz Information
Rate this Article :

1

2

3

4

5

6

7

8

9

10
Poor Excellent