Home arrow PHP arrow Page 3 - Output Buffering

Code Explained - PHP

Output control (or output buffering) allows you to write and execute your scripts normally but send data to the web browser at selected times. The main benefit of this system is that you can call the header(), setcookie() and session_start() functions at any place in your scripts without having to worry about the "headers already sent" error message.

TABLE OF CONTENTS:
  1. Output Buffering
  2. Login Script
  3. Code Explained
  4. Further suggestions
By: David Web
Rating: starstarstarstarstar / 4
September 02, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Notice that I’ve highlighted the functions that are used for output buffering. To begin output buffering, we use the ob_start() function, which is located at the very top of the page:


<?

ob_start();

session_start();


//someone registered?

if(isset($_GET['reg'])){

$reg="Your details have been added, please login";

}

$error=false;

$errmsg="";


//has form been submitted

if(isset($_POST['key'])){

//check that the username and password is not empty

if( empty($_POST['uname']) && (empty($_POST['upass']))){

print "Please enter your username and password.";

$errmsg="Please enter your username and password.";


Once you’ve called the ob_start() function, every single output for this page will be sent to a memory buffer rather than to the web browser. HTTP calls, like the header() and setcookie(), won't be buffered and will operate as usual.

Towards the end of the script you will notice the ob_end_flush() function:

<tr>

<td>&nbsp;</td>

<td><input type="submit" name="submit" value="Login"></td>

</tr>

</table>

</form>

<!-- InstanceEndEditable --></td>

</tr>

<tr>

<td class="copy">&copy;2008</td>

</tr>

</table>

</body>

<!-- InstanceEnd --></html>

<? ob_end_flush(); ?>


This function will take the accumulated buffer and send it to the web browser. You can also use the ob_end_clean() function; it does the same thing. Both of the functions turn off output buffering.

From a developer’s point of view, there is not so much of a benefit except that once you start using these functions, you really don’t have to spend any time worrying about HTTP headers. When you use templates in your applications, it would be beneficial to place these functions at the top (ob_start() and ob_end_flush() at the bottom). That way, these functions will be available on every page of your site.



 
 
>>> More PHP Articles          >>> More By David Web
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 2 - Follow our Sitemap

Dev Shed Tutorial Topics: