Home arrow PHP arrow Output Buffering

Output Buffering

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

The tools

In our battle against the "headers already sent"error message, we are going to use several functions that are natively available to PHP:

header()-is used to send a raw HTTP header.

Syntax-( string $string [, bool $replace [, int $http_response_code]] )

setcookie() -defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent beforeany output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including <html>and <head>tags as well as any whitespace.

Syntax- ( string $name [, string $value [, int $expire [, string $path [, string $domain [, bool $secure [, bool $httponly]]]]]] )

session_start() -creates a session or resumes the current one based on the current session id that's being passed via a request, such as GET, POST, or a cookie.

Syntax– session_start()

ob_start()-This function will turn output buffering on. While output buffering is active, no output is sent from the script (other than headers). Instead, the output is stored in an internal buffer.

Syntax– ob_start()

ob_end_flush() -This function will send the contents of the topmost output buffer (if any) and turn this output buffer off. If you want to further process the buffer's contents you have to call ob_get_contents()before ob_end_flush()as the buffer contents are discarded after ob_end_flush()is called.

Syntax– ob_end_flush()

ob_end_clean() -This function discards the contents of the topmost output buffer and turns off this output buffering. If you want to further process the buffer's contents you have to call ob_get_contents()before ob_end_clean()as the buffer contents are discarded when ob_end_clean()is called.

Syntax– ob_end_clean()



 
 
>>> 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: