ob_start() - Output Buffering Function

This section describes how to use the ob_start() function to turn on output buffering, which allows HTTP response header lines to be added after response body has been added.

As you can see from setcookie() definition, the PHP engine provides no buffer for the HTTP response body. That means as soon the PHP script starts to send output to the HTTP response body, the HTTP header block will be finalized, and not allowed to change any more.

But this default behavior can be altered by calling output control functions:

Of course, default behavior can also be altered by the configuration file, php.ini. Open php.ini and set the following line:

output_buffering = 4096

The above configuration line tells the PHP engine to turn on output buffering, and set the buffer size to 4096 bytes. Once "output_buffering" is turned on, you don't have to call ob_start() in your scripts.

To test the PHP engine default behavior, I modified CookieTest.php into CookieOutputBuffer.php:

#  CookieOutputBuffer.php
#- Copyright (c) 2003-2019, HerongYang.com, All Rights Reserved.
   print("Adding cookies by the server:\n");

   $numCookies = count( array_keys($_COOKIE) );
   $cookieName = "Cookie_$numCookies";
   $cookieValue = "My cookie value";
   print("   $cookieName: $cookieValue\n");

   setcookie($cookieName, $cookieValue);

   print("\nCookies received by the server:\n");
   foreach ($_COOKIE as $k => $v) {
      print "   $k = $v\n";
   print "</pre>\n";

I then opened php.ini and set the following line:

output_buffering = 0

Running IE on CookieOutputBuffer.php gave me this:

Adding cookies by the server:
   Cookie_2: My cookie value

Cookies received by the server:
   User = Herong Yang

PHP Warning: Cannot modify header information - headers already 
sent by (output started at ...\CookieOutputBuffer.php:4) ...

Now I truly beblieve that PHP engine's default behavior is no output buffering. Make sure to change "output_buffering" back to 4096 before continuing to the next test.

Last update: 2019.

Table of Contents

 About This Book

 Introduction and Installation of PHP 7.3

 PHP Script File Syntax

 PHP Data Types and Data Literals

 Variables, References, and Constants

 Expressions, Operations and Type Conversions

 Conditional Statements - "if" and "switch"

 Loop Statements - "while", "for", and "do ... while"

 Function Declaration, Arguments, and Return Values

 Arrays - Ordered Maps

 Introduction of Class and Object

 Integrating PHP with Apache Web Server

 Retrieving Information from HTTP Requests

 Creating and Managing Sessions in PHP Scripts

Sending and Receiving Cookies in PHP Scripts

 What Is a Cookie

 Sending and Receiving Cookies

 Sending and Receiving Cookies - Example

ob_start() - Output Buffering Function

 Persistent Cookies Saved on Hard Disk

 Other Cookie Properties - Domain and Path

 Controlling HTTP Response Header Lines in PHP Scripts

 MySQL Server Connection and Access Functions

 Functions to Manage Directories, Files and Images

 SOAP Extension Function and Calling Web Services

 SOAP Server Functions and Examples

 Localization Overview of Web Applications

 Using Non-ASCII Characters in HTML Documents

 Using Non-ASCII Characters as PHP Script String Literals

 Receiving Non-ASCII Characters from Input Forms

 "mbstring" Extension and Non-ASCII Encoding Management

 Managing Non-ASCII Character Strings with MySQL Servers

 Configuring and Sending out Emails

 Outdated Tutorials


 Full Version in PDF/EPUB