PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

output_add_rewrite_var> <ob_list_handlers
Last updated: Fri, 05 Sep 2008

view this page in

ob_start

(PHP 4, PHP 5)

ob_startTurn on output buffering

Description

bool ob_start ([ callback $output_callback [, int $chunk_size [, bool $erase ]]] )

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.

The contents of this internal buffer may be copied into a string variable using ob_get_contents(). To output what is stored in the internal buffer, use ob_end_flush(). Alternatively, ob_end_clean() will silently discard the buffer contents.

Warning

Some web servers (e.g. Apache) change the working directory of a script when calling the callback function. You can change it back by e.g. chdir(dirname($_SERVER['SCRIPT_FILENAME'])) in the callback function.

Output buffers are stackable, that is, you may call ob_start() while another ob_start() is active. Just make sure that you call ob_end_flush() the appropriate number of times. If multiple output callback functions are active, output is being filtered sequentially through each of them in nesting order.

Parameters

output_callback

An optional output_callback function may be specified. This function takes a string as a parameter and should return a string. The function will be called when ob_end_flush() is called, or when the output buffer is flushed to the browser at the end of the request. When output_callback is called, it will receive the contents of the output buffer as its parameter and is expected to return a new output buffer as a result, which will be sent to the browser. If the output_callback is not a callable function, this function will return FALSE.

If the callback function has two parameters, the second parameter is filled with a bit-field consisting of PHP_OUTPUT_HANDLER_START, PHP_OUTPUT_HANDLER_CONT and PHP_OUTPUT_HANDLER_END.

If output_callback returns FALSE original input is sent to the browser.

The output_callback parameter may be bypassed by passing a NULL value.

ob_end_clean(), ob_end_flush(), ob_clean(), ob_flush() and ob_start() may not be called from a callback function. If you call them from callback function, the behavior is undefined. If you would like to delete the contents of a buffer, return "" (a null string) from callback function. You can't even call functions using the output buffering functions like print_r($expression, true) or highlight_file($filename, true) from a callback function.

Note: In PHP 4.0.4, ob_gzhandler() was introduced to facilitate sending gz-encoded data to web browsers that support compressed web pages. ob_gzhandler() determines what type of content encoding the browser will accept and will return its output accordingly.

chunk_size

If the optional parameter chunk_size is passed, the buffer will be flushed after any output call which causes the buffer's length to equal or exceed chunk_size . Default value 0 means that the function is called only in the end, other special value 1 sets chunk_size to 4096.

erase

If the optional parameter erase is set to FALSE, the buffer will not be deleted until the script finishes (as of PHP 4.3.0).

Return Values

Returns TRUE on success or FALSE on failure.

ChangeLog

Version Description
4.3.2 This function was changed to return FALSE in case the passed output_callback can not be executed.

Examples

Example #1 User defined callback function example

<?php

function callback($buffer)
{
  
// replace all the apples with oranges
  
return (str_replace("apples""oranges"$buffer));
}

ob_start("callback");

?>
<html>
<body>
<p>It's like comparing apples to oranges.</p>
</body>
</html>
<?php

ob_end_flush
();

?>

The above example will output:

<html>
<body>
<p>It's like comparing oranges to oranges.</p>
</body>
</html>



output_add_rewrite_var> <ob_list_handlers
Last updated: Fri, 05 Sep 2008
 
add a note add a note User Contributed Notes
ob_start
fordiman at gmail dot com
27-Jun-2008 05:35
Here's a nifty function I use daily.  Essentially: include a PHP file - but render its output to a variable, rather than to the buffer.  It's also set up to load the script with a variable set, and automagically loads globals into the script's namespace, making it an effective templating scheme.  It also has error handling, so that you're not flying blind when using output buffering.

<?php
$GLOBALS
['BufferedErrors']=Array();
function
errorParse($errno, $errstr, $errfile, $errline, $errcontext) {
   
$errorTypes = Array(
       
E_ERROR => 'Fatal Error',
       
E_WARNING => 'Warning',
       
E_PARSE => 'Parse Error',
       
E_NOTICE => 'Notice',
       
E_CORE_ERROR => 'Fatal Core Error',
       
E_CORE_WARNING => 'Core Warning',
       
E_COMPILE_ERROR => 'Compilation Error',
       
E_COMPILE_WARNING => 'Compilation Warning',
       
E_USER_ERROR => 'Triggered Error',
       
E_USER_WARNING => 'Triggered Warning',
       
E_USER_NOTICE => 'Triggered Notice',
       
E_STRICT => 'Deprecation Notice',
       
E_RECOVERABLE_ERROR => 'Catchable Fatal Error'
   
);
   
$ret=(object)Array(
       
'number'=>$errno,
       
'message'=>$errstr,
       
'file'=>$errfile,
       
'line'=>$errline,
       
'context'=>$errcontext,
       
'type'=>$errorTypes[$errno]
    );
   
$GLOBALS['BufferedErrors'][]=$ret;
    return
false;
}
function
parse($fileToInclude, $argumentsToFile=false) {
   
$bufferedErrorStack = $GLOBALS['BufferedErrors'];
   
set_error_handler('errorParse', error_reporting());
   
$GLOBALS['BufferedErrors']=Array();
   
    if (!
file_exists($fileToInclude))
        return
'';
    if (
$argumentsToFile === false)
       
$argumentsToFile = Array();
   
$argumentsToFile = array_merge($GLOBALS, $argumentsToFile);
    foreach (
$argumentsToFile as $variableName => $variableValue)
        $
$variableName = $variableValue;
   
ob_start();
    include(
$fileToInclude);
   
$ret = ob_get_contents();
   
ob_end_clean();
   
   
restore_error_handler();
   
$errors = $GLOBALS['BufferedErrors'];
   
$GLOBALS['BufferedErrors'] = $bufferedErrorStack;
    if (
count($errors)>0) {
       
$ret.='<ul class="error">';
        foreach (
$errors as $error)
           
$ret.=
               
'<li>'.
                   
'<b>'.$error->type.'</b>: '.
                   
$error->message.
                   
'<blockquote>'.
                       
'<i>file</i>: '.$error->file.'<br />'.
                       
'<i>line</i>: '.$error->line.
                   
'</blockquote>'.
               
'</li>';
       
$ret.='</ul>';
    }
    return
$ret;
}
petethebloke at gmail dot com
25-Apr-2008 01:45
I was seriously stuck trying to get marketing emails into a site so that the designer could edit them in Dreamweaver while my app could dynamically write and send them to customers. Then I discovered that this works - I barely believed how clever PHP could be! Hope this helps someone else.
<?php
class marketing_email{
    function
marketing_email($customer_id){
       
//---stuff to get customer details from DB-------
        //----stuff---
        //I'm going to put 'newpart-' at the beginning and 'a' at the end of the separator string
        //I find that the 'a' is more reliable for a line end when it arrives at the client computer
       
$random_hash = md5(date('r', time()));
       
ob_start(); //Turn on output buffering
       
?>
--newpart-<?php echo $random_hash; ?>a
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Dear <?php echo $customer_name ?>
Your New special offer Blah Blah

--newpart-<?php echo $random_hash; ?>a
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<head>
<style type="text/css">
<!--
p {
font-family: Verdana, Arial, Helvetica, sans-serif;
etc etc
}
-->
</style>
<title>Offer</title>
</head>
<body>
Blah Blah Blah
</body>

--newpart-<?php echo $random_hash; ?>a--
        <?php
        $pagecontent
=ob_get_contents();
       
ob_end_clean();
       
//define the headers we want passed. Note that they are separated with \r\n
       
$mail_headers = "MIME-Version: 1.0\r\nFrom: admin@domain.co.uk\r\nReply-To: admin@domain.co.uk";
       
//add boundary string and mime type specification
       
$mail_headers .= "\r\nContent-Type: multipart/alternative;\r\n\tboundary=\"newpart-".$random_hash."a\"";
       
$to=$customer_email;

        if(
mail($to,"Offer",$pagecontent,$mail_headers)){
            return
TRUE;
        }else{
            return
FALSE;
        }
    }
}
?>
Scott
16-Apr-2008 01:15
Hello,

I wrote a pretty simple, yet very effective page caching script using output buffering.

Just one of the uses for such a collection of functions.

http://www.coderprofile.com/source-code/170/simple-page-caching

Kind regards,
Scott
coldshine at gmail dot com
07-Apr-2008 07:52
Referring to dan at roteloftet dot com's comment:

RFC 2616 (HTTP) specifies a "transparent" Content-Encoding, "identity" (§ 3.5), that nicely suits what you tried to do with the (invalid) "None". So this equally working, and it's also RFC-compliant:

<?php
header
('Content-Encoding: identity', true);
?>
dan at roteloftet dot com
09-Mar-2008 10:19
Some web hosting servers (mine do, at least) have in their php.ini the following setting:
output_handler = ob_gzhandler

This proved problematic for php-scripts which returns an image or a binary file in general, since there is no way to determine the content length of the compressed file.

Since I spent a lot of time scouring the net searching for a work-around (.htaccess-modifications were out of the picture for various reasons), I found this to work nicely to cancel out the ob_gzhandler specified in the php.ini:

<?php
while (ob_get_level())
       
ob_end_clean();
header("Content-Encoding: None", true);
?>

Put this at the top of the script before anything else is written to the page, and the script result will not be compressed.
dale3h
10-Jan-2008 03:06
Here's a simple way, using register_shutdown_function, to easily maintain your global output filter:

File: outputfilter.inc.php
<?php
  initOutputFilter
();

  function
initOutputFilter() {
   
ob_start('cbOutputFilter');
   
register_shutdown_function('ob_end_flush');
  }

  function
cbOutputFilter($output) {
   
$search  = array(
     
'user@domain.com',
     
'user2@domain.com',
    );
   
$replace = array(
     
'<img src="/images/nospam001.jpg" border="0" alt="" />',
     
'<img src="/images/nospam002.jpg" border="0" alt="" />',
    );

    return
str_replace($search, $replace, $output);
  }
?>

Just include it in your global file, or in each file you want to filter.

This one in particular replaces any occurrence of "user@domain.com" or "user2@domain.com" with an image (that contains each email respectively), preventing spam bots from picking it up.
Eamon Straughn eamon at gizzle dot co dot uk
27-Dec-2007 07:06
In regards to below. The best thing to do is create an error handler that catches all non-fatal errors prior and during your ob_start.

works for me all the time. and if there is an error you can format the errors in that script it executed in. From my experience i'm quite sure that would be the most logical choice. wouldn't it? Plus your using objects...Please do think before you can code.

Always plan else you'll be working blind. Anyways ob_start wouldn't exit;

Just plan ahead and anticipate. It's just like driving a car.. Happy new year.
Anonymous
22-Oct-2007 02:40
As a follow up to my previous post :
(output seems to go to standard ouptput in command  line mode even with the use of output buffering (ob_start) )

Setting implicit_flush to false seems to do the trick :

   ini_set('implicit_flush',false); // (avoids output even with    ob_start, in command line mode)
   ob_start();
   include (realpath(dirname(__FILE__))."/".$template);
   $ret_str.=ob_get_contents();
   ob_end_clean();
   ini_set('implicit_flush',true);

(See :
Chapter 43. Using PHP from the command line
)
Francois Hill
22-Oct-2007 01:42
Following clement dot ayme at st dot com 's remark :

In my experience it seems that the output IS buffered, but ALSO sent to the standard output !
Charlie Farrow
18-Oct-2007 05:49
Under certain freak conditions, when an error ocours perfoming an action on an object that cannot be done (either because the object does not exist or the method does not exist) inside of an ob_start() the script will exit and print everything the current function generates before the error, but nothing else, including no error message.

I am at a loss to why no error message appears and am trying to get a working example for the developers that is simpler than my whole program!

So if you are using ob_start() and you get no output, check your objects.... you have made a mistake on them somewhere. The only trouble is you will not know where as there is no error!!
Asher Haig (ahaig at ridiculouspower dot com)
20-Aug-2007 09:17
When a script ends, all buffered output is flushed (this is not a bug: http://bugs.php.net/bug.php?id=42334&thanks=4). What happens when the script throws an error (and thus ends) in the middle of an output buffer? The script spits out everything in the buffer before printing the error!

Here is the simplest solution I have been able to find. Put it at the beginning of the error handling function to clear all buffered data and print only the error:

$handlers = ob_list_handlers();
while ( ! empty($handlers) )    {
    ob_end_clean();
    $handlers = ob_list_handlers();
}
clement dot ayme at st dot com
27-Jul-2007 05:51
ob_start() seems not compliant with command-line PHP.

just calling the function ahead of your unix-php script and nothing
happens. The STDOUT stream is not buffered.
tracey AT archive DOT org
29-Mar-2007 10:01
Way to make all stdout and stderr write to a log
from *inside* a php script.
You simply need to make sure to call elog() every
once in awhile to get output.
It's a nice way to "daemonize" a script w.r.t. its logging.

// This allows us to capture all stdout and stderr (and error_log() calls)
// to this logfile...
// The "collected output" will be flushed anytime "elog()" is used...
ini_set("error_log", "/var/log/script.log");
ob_start();

function elog($str)
{
  // get anything written to stdout or stderr that did *NOT* use elog()
  // and write it now...
  $writeme = ob_get_contents();
  if ($writeme)
  {
    error_log($writeme);
    ob_end_clean();
    ob_start();
  }
  // now write message this method was called with
  error_log($str);
}
joebezucha at tlen dot pl
16-Mar-2007 10:05
Hi, I use those functions for stripping unnecessary chars in my output code...because I have JavaScript placed in outpout code so I don't remove \n\r\t but just replace them with single space (it could cause errors in scripts)
Function stripBufferSkipTextareaTags skips tags Textarea. It's needed to don't loose \n\r when user edit some content...

sorry for my english ;)

<?php

function stripBufferSkipTextareaTags($buffer){
   
$poz_current = 0;
   
$poz_end = strlen($buffer)-1;
   
$result = "";
   
    while (
$poz_current < $poz_end){
       
$t_poz_start = stripos($buffer, "<textarea", $poz_current);
        if (
$t_poz_start === false){
           
$buffer_part_2strip = substr($buffer, $poz_current);
           
$temp = stripBuffer($buffer_part_2strip);
           
$result .= $temp;
           
$poz_current = $poz_end;
        }
        else{
           
$buffer_part_2strip = substr($buffer, $poz_current, $t_poz_start-$poz_current);
           
$temp = stripBuffer($buffer_part_2strip);
           
$result .= $temp;
           
$t_poz_end = stripos($buffer, "</textarea>", $t_poz_start);
           
$temp = substr($buffer, $t_poz_start, $t_poz_end-$t_poz_start);
           
$result .= $temp;
           
$poz_current = $t_poz_end;
        }
    }
    return
$result;
}

function
stripBuffer($buffer){
   
// change new lines and tabs to single spaces
   
$buffer = str_replace(array("\r\n", "\r", "\n", "\t"), ' ', $buffer);
   
// multispaces to single...
   
$buffer = ereg_replace(" {2,}", ' ',$buffer);
   
// remove single spaces between tags
   
$buffer = str_replace("> <", "><", $buffer);
   
// remove single spaces around &nbsp;
   
$buffer = str_replace(" &nbsp;", "&nbsp;", $buffer);
   
$buffer = str_replace("&nbsp; ", "&nbsp;", $buffer);
    return
$buffer;
}

ob_start("stripBufferSkipTextareaTags");

?>
denis at SPAM_WELCOME dot i39 dot ru
13-Mar-2007 02:55
This function's behaviour  has been changed in php 5.2.0:

<?
global $AP;
$AP = new ap;
ob_start("ob_end");

function
ob_end() {
        global
$AP;
       
$r = $AP->test();

        return
$r;
}

class
ap {
        function
test() {
                return
"debug";
        }
}
?>

In older versions it shows: "debug".
But latest php version causes error: PHP Fatal error: Call to a member function test() on a non-object.
And this is not a bug: http://bugs.php.net/bug.php?id=40104
Tobias Goldkamp
25-Dec-2006 11:02
I use this to strip unnecessary characters from HTML output:

<?php

function sanitize_output($buffer)
{
   
$search = array(
       
'/\>[^\S ]+/s', //strip whitespaces after tags, except space
       
'/[^\S ]+\</s', //strip whitespaces before tags, except space
       
'/(\s)+/s'  // shorten multiple whitespace sequences
       
);
   
$replace = array(
       
'>',
       
'<',
       
'\\1'
       
);
 
$buffer = preg_replace($search, $replace, $buffer);
    return
$buffer;
}

ob_start("sanitize_output");

?>
lucky760 at yahoo dot com
21-Nov-2006 05:20
Just for simplicity's sake (and because I had to rewrite it for use at http://www.VideoSift.com anyway), here's a very simplified, pre-PHP5 version. Just add one call to dump_css_cache() for each of your CSS files.

<?php

ob_start
('ob_gzhandler');

header('Content-Type: text/css; charset: UTF-8');
header('Cache-Control: must-revalidate');

$expire_offset = 0; // set to a reaonable interval, say 3600 (1 hr)
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $expire_offset) . ' GMT');

function
css_compress($buffer) {
 
$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);// remove comments
 
$buffer = str_replace(array("\r\n", "\r", "\n", "\t", '  '), '', $buffer);// remove tabs, spaces, newlines, etc.
 
$buffer = str_replace('{ ', '{', $buffer);// remove unnecessary spaces.
 
$buffer = str_replace(' }', '}', $buffer);
 
$buffer = str_replace('; ', ';', $buffer);
 
$buffer = str_replace(', ', ',', $buffer);
 
$buffer = str_replace(' {', '{', $buffer);
 
$buffer = str_replace('} ', '}', $buffer);
 
$buffer = str_replace(': ', ':', $buffer);
 
$buffer = str_replace(' ,', ',', $buffer);
 
$buffer = str_replace(' ;', ';', $buffer);
  return
$buffer;
}

function
dump_css_cache($filename) {
 
$cwd = getcwd() . DIRECTORY_SEPARATOR;

 
$stat = stat($filename);
 
$current_cache = $cwd . '.' . $filename . '.' . $stat['size'] . '-' . $stat['mtime'] . '.cache';

 
// the cache exists - just dump it
 
if (is_file($current_cache)) {
    include(
$current_cache);
    return;
  }

 
// remove any old, lingering caches for this file
 
if ($dead_files = glob($cwd . '.' . $filename . '.*.cache', GLOB_NOESCAPE))
    foreach (
$dead_files as $dead_file)
     
unlink($dead_file);
 
  if (!
function_exists('file_put_contents')) {
    function
file_put_contents($filename, $contents) {
     
$handle = fopen($filename, 'w');
     
fwrite($handle, $contents);
     
fclose($handle);
    }
  }
 
 
$cache_contents = css_compress(file_get_contents($filename));
 
file_put_contents($current_cache, $cache_contents);
 
  echo
$cache_contents;
}

dump_css_cache('_general.css');

?>
lucky760 at yahoo dot com
21-Nov-2006 05:10
In extension to the compress() function posted below, here's a nifty little class that improves the idea a bit. Basically, running that compress() function for all your CSS for every single page load is clearly far less than optimal, especially since the styles will change only infrequently at the very worst.

With this class you can simply specify an array of your CSS file names and call dump_style(). The contents of each file are saved in compress()'d form in a cache file that is only recreated when the corresponding source CSS changes.

It's intended for PHP5, but will work identically if you just un-OOP everything and possibly define file_put_contents.

Enjoy!

<?php

$CSS_FILES
= array(
 
'_general.css'
);

$css_cache = new CSSCache($CSS_FILES);
$css_cache->dump_style();

//
// class CSSCache
//

class CSSCache {
  private
$filenames = array();
  private
$cwd;
   
  public function
__construct($i_filename_arr) {
    if (!
is_array($i_filename_arr))
     
$i_filename_arr = array($i_filename_arr);
   
   
$this->filenames = $i_filename_arr;
   
$this->cwd = getcwd() . DIRECTORY_SEPARATOR;
   
    if (
$this->style_changed())
     
$expire = -72000;
    else
     
$expire = 3200;
   
   
header('Content-Type: text/css; charset: UTF-8');
   
header('Cache-Control: must-revalidate');
   
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $expire) . ' GMT');
  }
 
  public function
dump_style() {
   
ob_start('ob_gzhandler');
   
    foreach (
$this->filenames as $filename)
     
$this->dump_cache_contents($filename);
   
   
ob_end_flush();
  }
 
    private function
get_cache_name($filename, $wildcard = FALSE) {
   
$stat = stat($filename);
    return
$this->cwd . '.' . $filename . '.' .
      (
$wildcard ? '*' : ($stat['size'] . '-' . $stat['mtime'])) . '.cache';
  }
 
  private function
style_changed() {
    foreach (
$this->filenames as $filename)
      if (!
is_file($this->get_cache_name($filename)))
        return
TRUE;
    return
FALSE;
  }

  private function
compress($buffer) {
   
$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
   
$buffer = str_replace(array("\r\n", "\r", "\n", "\t", '  '), '', $buffer);
   
$buffer = str_replace('{ ', '{', $buffer);
   
$buffer = str_replace(' }', '}', $buffer);
   
$buffer = str_replace('; ', ';', $buffer);
   
$buffer = str_replace(', ', ',', $buffer);
   
$buffer = str_replace(' {', '{', $buffer);
   
$buffer = str_replace('} ', '}', $buffer);
   
$buffer = str_replace(': ', ':', $buffer);
   
$buffer = str_replace(' ,', ',', $buffer);
   
$buffer = str_replace(' ;', ';', $buffer);
    return
$buffer;
  }
 
  private function
dump_cache_contents($filename) {
   
$current_cache = $this->get_cache_name($filename);
   
   
// the cache exists - just dump it
   
if (is_file($current_cache)) {
      include(
$current_cache);
      return;
    }
   
   
// remove any old, lingering caches for this file
   
if ($dead_files = glob($this->get_cache_name($filename, TRUE), GLOB_NOESCAPE))
      foreach (
$dead_files as $dead_file)
       
unlink($dead_file);
   
   
$compressed = $this->compress(file_get_contents($filename));
   
file_put_contents($current_cache, $compressed);
   
    echo
$compressed;
  }
}

?>
10-Nov-2006 06:34
I'm sure some of you more brilliant minds could pare this down some more, but using the method found at fiftyfoureleven.com  for compressing, I got my 10090-byte stylesheet down to 3536 bytes and then again down to 2713 bytes, by stripping unecessary characters from the stylesheet. 2 ob_start calls and the CSS file is now 73% smaller. YMMV.

<?php
ob_start
("ob_gzhandler");
ob_start("compress");
header("Content-type: text/css; charset: UTF-8");
header("Cache-Control: must-revalidate");
$off = 0; # Set to a reaonable value later, say 3600 (1 hr);
$exp = "Expires: " . gmdate("D, d M Y H:i:s", time() + $off) . " GMT";
header($exp);

function
compress($buffer) {
   
$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer); // remove comments
   
$buffer = str_replace(array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), '', $buffer); // remove tabs, spaces, newlines, etc.
   
$buffer = str_replace('{ ', '{', $buffer); // remove unnecessary spaces.
   
$buffer = str_replace(' }', '}', $buffer);
   
$buffer = str_replace('; ', ';', $buffer);
   
$buffer = str_replace(', ', ',', $buffer);
   
$buffer = str_replace(' {', '{', $buffer);
   
$buffer = str_replace('} ', '}', $buffer);
   
$buffer = str_replace(': ', ':', $buffer);
   
$buffer = str_replace(' ,', ',', $buffer);
   
$buffer = str_replace(' ;', ';', $buffer);
    return
$buffer;
}

require_once(
'screen.css');
require_once(
'layout.css');
require_once(
'custom.php');
require_once(
'titles.css');
require_once(
'bus.css');

?>
feedback at realitymedias dot com
07-Nov-2006 11:35
PHP Suggests: Some web servers (e.g. Apache) change the working directory of a script when calling the callback function. You can change it back by doing, for example, the following in the callback function: chdir(dirname($_SERVER['SCRIPT_FILENAME']))

The solution provided by PHP, does not function as intended when running PHP as a CGI (on CGI mode (CGI-BUILD (--enable-cgi) and/or CLI)).  In such a case, PHP is executed as a CGI-BIN and the web server daemon (e.g. Apache) sees SCRIPT_FILENAME as being the PHPCGI processor, and won't look deeper to find what file the PHPCGI processor is actually running/parsing; therefore the path returned by SCRIPT_FILENAME is wrong (most of the time, containing/ending with "cgi-system/php.cgi").

As SCRIPT_FILENAME is the safest way to proceed, but turns to be wrong in this exact situation; PATH_TRANSLATED is the next safe solution one would turn towards since it is populated with a different mechanism.

It would be correct to develop in the direction if the script filename itself is contained in the SCRIPT_FILENAME path value, then the SCRIPT_FILENAME content is reported correctly. If it is not, using PATH_TRANSLATED is the next logical choice we can use. The best reference in this case would be PHP_SELF as it is populated by PHP itself. Using SCRIPT_NAME as a reference would be an error as it is affected by the same problem (reports cgi-system and/or php.cgi as well).

The following is the revised code and should work on both the non-CGI and the CGI PHP processor types.

<?php
chdir
(dirname((strstr($_SERVER["SCRIPT_FILENAME"], $_SERVER["PHP_SELF"])
?
$_SERVER["SCRIPT_FILENAME"] : $_SERVER["PATH_TRANSLATED"])));
?>

Or the decomposed code as follows:

<?php
if (strstr($_SERVER["SCRIPT_FILENAME"], $_SERVER["PHP_SELF"])) {
$reference = $_SERVER["SCRIPT_FILENAME"];
} else {
   
$reference = $_SERVER["PATH_TRANSLATED"];
}
chdir(dirname($reference);
?>

This has been tested on Apache 1 & 2, PHP 4 & 5 and IIS 5.1
butch at enterpol dot pl
02-Oct-2006 08:04
simple code to make phpsession $_GET nice for Valid XHTML 1.0 Transitional :)

function callback($buffer)
{
  $buffer = str_replace("&PHPSESSID", "&amp;PHPSESSID", $buffer);
  return $buffer;
}

ob_start("callback");

session_start();
net_navard at yahoo dot com
30-May-2006 03:09
Hello firends

ob_start() opens a buffer in which all output is stored. So every time you do an echo, the output of that is added to the buffer. When the script finishes running, or you call ob_flush(), that stored output is sent to the browser (and gzipped first if you use ob_gzhandler, which means it downloads faster).

The most common reason to use ob_start is as a way to collect data that would otherwise be sent to the browser.

These are two usages of ob_start():

1-Well, you have more control over the output. Trivial example: say you want to show the user an error message, but the script has already sent some HTML to the browser. It'll look ugly, with a half-rendered page and then an error message. Using the output buffering functions, you can simply delete the buffer and sebuffer and send only the error message, which means it looks all nice and neat buffer and send
2-The reason output buffering was invented was to create a seamless transfer, from: php engine -> apache -> operating system -> web user

If you make sure each of those use the same buffer size, the system will use less writes, use less system resources and be able to handle more traffic.

With Regards, Hossein
(capitals) THE maYoR ANd thOSe@gmail
31-Jan-2006 10:14
One of the notes below mentions that ob_end_flush() is called automatically at the end of the script if you called ob_start without an ob_end.

Because I couldn't find any other way to do it, I tried to use this fact to have some stuff run at the end of every script. It was a maintenance nightmare, so I'm putting a link here to the good way to do it, since it's nigh impossible to find with google.

http://php.net/register_shutdown_function
simon
24-Jan-2006 04:51
Found that variables in class instances we're not being set after the call to ob_start().
Call ob_start after the variables are set however and it works but that didn't seem to solve the goal of a self contained templating class.
The fix was to assign the class by reference with '&new'
Here is a simplified working example:
<?php
class Buffer {
var
$template = ' - template set in class constructor';
function
Buffer() {
   
$this->startBuffer();
}
function
startBuffer() {
   
ob_start(array(&$this, 'doFlush'));
}
function
doFlush($buffer) {
   
/* simple string concat to show use of a
    template string and the buffer output */
   
return $buffer . $this->template;
}
}
/* template does not get set:
$buffer1 = new Buffer();
$buffer1->template = ' - template set in instance';
echo 'some buffer content';
*/
/* this works as expected */
$buffer2 = &new Buffer();
$buffer2->template = ' - template set in instance';
echo
'some buffer content';
ernest at vogelsinger dot at
08-Jan-2006 05:57
When you rely on URL rewriting to pass the PHP session ID you should be careful with ob_get_contents(), as this might disable URL rewriting completely.

Example:
ob_start();
session_start();
echo '<a href=".">self link</a>';
$data = ob_get_contents();
ob_end_clean();
echo $data;

In the example above, URL rewriting will never occur. In fact, rewriting would occur if you ended the buffering envelope using ob_end_flush(). It seems to me that rewriting occurs in the very same buffering envelope where the session gets started, not at the final output stage.

If you need a scenario like the one above, using an "inner envelope" will help:

ob_start();
ob_start();   // add the inner buffering envelope
session_start();
echo '<a href=".">self link</a>';
ob_end_flush(); // closing the inner envelope will activate URL rewriting
$data = ob_get_contents();
ob_end_clean();
echo $data;

In case you're interested or believe like me that this is rather a design flaw instead of a feature, please visit bug #35933 (http://bugs.php.net/bug.php?id=35933) and comment on it.
cyrille.berliat[no spam]free.fr
16-Oct-2005 11:07
If you're trying to use ob_start() in some PHP5 classes (probably works on PHP4 classes), this is the good way :

<?

class HTMLPage
{
//----------------------------------------------------------------- PUBLIC

//----------------------------------------------------- Méthodes publiques
   
   
public static function ConvertIntoSGML( $source )
   
// Mode d'emploi :
    //convertit une string en une SGML valide
    //
    // Renvoie :
    //la chaine traitée
    //
    // Algorithme :
    //analyse char par char de la chaine. Si un caractère est de nombre ASCII > 127,
    //conversion en son code SGML.
   
{
       
$newString = '';
       
        for(
$i = 0; $i < strlen( $source ) ; $i++ ) {
           
$o = ord( $source{ $i } );
           
           
$newString .= ( ( $o > 127 ) ? '&#'.$o.';': $source{ $i } );
        }
       
        return
$newString;
    }
   
    public function
FlushSite( $source )
    {
        return
$this->ConvertIntoSGML ( $source );
    }
   
//-------------------------------------------- Constructeurs - destructeur
   
function __construct()
   
// Mode d'emploi (constructeur) :
    //initialise la buffurisation
    //
    // Contrat :
    //
   
{
       
ob_start( array (  & $this , 'FlushSite' ) );
    }
//---- Fin du constructeur
   
//------------------------------------------------------ Méthodes Magiques

//------------------------------------------------------------------ PRIVE

}

// Example :

$webdesign = new HTMLPage ( );

echo
'Héllo world'; // Will produce the source 'h&#233;llo world'

?>

Without the & before $this, you'll loose your content because ob_start() will call the flushsite() function from a clone of the object and not the caller object himself.

Note : call_back function must be public because ob_start() is in an extern scope from your class :)

I hope this will help you!
php at bucksvsbytes dot com
16-Sep-2005 06:29
The following should be added: "If outbut buffering is still active when the script ends, PHP outputs it automatically. In effect, every script ends with ob_end_flush()."
admin at bobfrank dot org
28-Aug-2005 04:50
If you want to run code in the middle of a string that you made, but you want to wait the printing...
(so if you want to allow php in bb-code style, and you want to execute it in order, and print everything in order...)

phpRun($code) {
    ob_start();
    exec($code);
    $output = ob_get_contents();
    ob_end_clean();
    return $output;
}

$str = str_replace("]\n", "]", $str);
$match = array('#\[php\](.*?)\[\/php\]#se');
$replace = array( phpRun( stripslashes('$1') ) );
$str= preg_replace($match, $replace, $str);

echo $str;
kasper at johansen dot tdcadsl dot dk
28-Aug-2005 03:29
Another way to make your code harder to copy, is to remove all line-breaks and tabs you have in it.

You can use this function to remove those.

You can choose to place "ob_start();" at the start of your main PHP-file, and "ob_end_clean();" at the end of it.

This is not the best solution though. If you are running Apache you might want to do something like this to a ".htaccess"-file:

php_value auto_prepend_file "/home/www/load_before.php"
php_value auto_append_file "/home/www/load_after.php"

(you should replace those files with files that actually exists).

In the "load_before.php" you can place the "ob_start();", and in the "load_after.php" you can do something like this:

<?
    $html
= strtr(ob_get_contents(), array("\t" => "", "\n" => "", "\r" => ""));
   
ob_end_clean();
   
    echo
$html;
?>

This will clean your HTML of all kind of linebreaks (both \n and \r) and tabulators (\t). This will save your users some bandwidth, but it will also make your HTML, JavaScripts and more very difficult to read or copy. That way, making it harder for people to steal your code if you do not want them to.

This isnt in the spirit of OpenSource, but anyway you should be aware that this is possible.

Be aware that if you use PHP-files, to simulate pictures, all linebreaks will also be remove, making them corrupt. A solution to this, could be to check the headers (Content-Type), and if isnt set, or it is "text/html", you can go ahead and remove the linebreaks and tabs.

At my site, with more than 50.000 pictures and about 60 people online, I couldnt see any difference in the loadtime.

But still be aware, that your output will not be sent, before the script is finished, which will make your page slower to load that way also, since it cannot send any output while loading, but will have to wait until the load is finished.
geoffrey at nevra dot net
09-Aug-2005 08:05
When using a callback with ob_start(), functions like ob_get_contents() don't make use of it, use ob_end_flush() instead.

nb: not tested with every ob_* functions, just ob_get_contents() and ob_end_flush()
05-Aug-2005 03:54
I usually create my pages in four parts - variable initialisation, import header (using the variables just declared to configure), main body (mostly non-PHP), import footer.  I wondered about making the main body examinable by another PHP script if the main page was included into it.  I found I could control output of the main body by ending the header with an unclosed function which finishes at the start of the footer, thus enclosing the main body.  Output buffering can then be used to read this into a variable.  As a demonstration of how this can be used to control the order of output look at this example:

<?php
$output
= "";

// Callback to process buffered output
function capture($buffer)
    {
   
$GLOBALS['output'] .= $buffer;
    return
"C ";
    }

// Calls the printE() function with output capture
function captureE()
    {
   
ob_start("capture");
   
printE();
   
ob_end_flush();
    }
?>

A
<?php
// Output 'E' (the main body in the example scenario)
function printE()
    {
// (End header after this line) ?>
E
    <?php // (Start footer with this line)
   
}
?>
B
<?php captureE(); ?>
D
<?php print $output; ?>
F
<?php printE(); ?>
G

The output is A B C D E F E G.

For the application I mentioned above there are two points to note:
 - The page when executed alone must output its main body but the inspection script should suppress this, perhaps by means of a variable set before the page is included and then checked for in the footer output lines.
 - Because the main body is now inside a function it has a different namespace, thus changes may be required to prevent code breaking (e.g. use of globals, handling of functions defined within the main body).
oersoep at gmail dot com
08-Jul-2005 01:46
These are handy. First one has been mentioned before.

ob_start( array( 'lib_class', 'parse_output' ) );
ob_start( array( $this, 'parse_output' ) );

Note: $this is NOT a reference. Anything the callback saves or logs disappears in the clone ob_start works with.
It does enable the callback to work with the attributes of $this, like $this->ar_tpl_value or whatever your style is.

The manual says:
"If the optional parameter chunk_size is passed, the callback function is called on every first newline after chunk_size bytes of output. The output_callback parameter may be bypassed by passing a NULL value."
This doesn't work with my 4.3.11. Might be the Zend optimizer though. Daren't turn it off to go see.
Aleksey
25-May-2005 08:08
This function dynamically changes title of HTML page:

  function change_title($new_title) {
    $output = ob_get_contents();
    ob_end_clean();

    $output = preg_replace("/<title>(.*?)<\/title>/", "<title>$new_title</title>", $output);
    echo $output;
  }

Example:
  ob_start();
  // ... some output
  change_title('NEW TITLE!');
jds1509 at NOSPAMrit dot edu
24-May-2005 03:06
This code demonstrates the affect of providing a value to the chunk_size parameter. A value of 1 or 0 will be ignored by php. Here's the Code:

<?

function callback($buffer)
{
    return
"TRAPPED:".$buffer."<br/>";
}

ob_start("callback",2);

echo
"long string, so callback";
echo
"X";
echo
" - no callback, less than 2 chars";

?>newlines,
but
no
callback
!<?

// PHP block

?>PHP block initiates callback.<?

echo "One more callback at EOF...";

?>

The code above outputs:

TRAPPED:long string, so callback
TRAPPED:X - no callback, less than 2 chars
TRAPPED:newlines, but no callback !
TRAPPED:PHP block initiates callback.
TRAPPED:One more callback at EOF...
TRAPPED:
rafa dot chacon at factorydea dot com
10-May-2005 10:10
If you're trying to include a php file inside a loop by require_once (in example, a dinamic email template) and change the value of some variables (in example, url to unsuscribe, different for each user), you should use

<?php

// ... some code

$usermail = array("email1", "email2", ...);

for(
$i = 0; $i < $MAX; $i++)
{
       
$usermail_unsuscribe = $usermail[$i];
       
ob_start();
        include(
"email_template.php");
       
ob_clean();
}
?>

Otherwise $usermail_unsuscribe will get only "email1" value.
JM
08-May-2005 08:17
I don't claim to understand this--I would have expected the exact opposite--but it seems that
  ob_start() ... ob_end_flush()
can massively improve perfomance, by at least a factor of 10 (admittedly a small number of samples).

I tried this after discovering that I could move a large (100ms) bottleneck in one of my scripts into
   echo "<!-- about 40 characters of junk -->";
which clearly shouldn't have taken long to run.

My unfounded theory is that without buffering, the interaction between PHP4.3.4 and Apache is not optimized, whereas with buffering, PHP delivers the entire page at once, which Apache handles better.

I should add that this is under https.
Ray Paseur (Paseur ... ImagineDB.com)
01-Mar-2005 09:50
You can use PHP to generate a static HTML page.  Useful if you have a complex script that, for performance reasons, you do not want site visitors to run repeatedly on demand.  A "cron" job can execute the PHP script to create the HTML page.  For example:

<?php // CREATE index.html
  
ob_start();
/* PERFORM COMLEX QUERY, ECHO RESULTS, ETC. */
  
$page = ob_get_contents();
  
ob_end_clean();
  
$cwd = getcwd();
  
$file = "$cwd" .'/'. "index.html";
   @
chmod($file,0755);
  
$fw = fopen($file, "w");
  
fputs($fw,$page, strlen($page));
  
fclose($fw);
   die();
?>
eddie
13-Feb-2005 04:09
I use this function for deleting not needed characters within the html code before sending the whole stuff to the browser.

function callback($buffer){
    $buffer = str_replace("\n", "", $buffer);
    $buffer = str_replace("\t", "", $buffer);
    $buffer = str_replace(chr(13), "", $buffer);
    $buffer = ereg_replace("<!\-\- [\/\ a-zA-Z]* \-\->", "", $buffer);
    return $buffer;
}

First str_replace will delete any newlines, second any tabs and the third any carriage return. Finally the regular expression will delete any html-comment which consists of /, space, a-z or A-Z.
Using this saves about 1kb on every pageload.
FB
02-Feb-2005 02:59
I've noticed a bug with MSIE for non cached contents if your page is less than 4096 octets : you have to refresh the page each time to view its content !

Here is the solution to prevent this stupid behaviour of MSIE : just insert this code at the top of your scripts :

function ob_callback($buffer)
{
    return $buffer . str_repeat(' ', max(0, 4097 - strlen($buffer)));
}

ob_start('ob_callback');
aaron at offtone.com
14-Nov-2004 01:19
My callback is stored in a function class, and using ob_start ('Class::callback') wasn't working. Not wanting to instantiate the class (no need, it's a function class) I tried this and it worked a charm:

ob_start (array (Class, 'callback'));