List Info

Thread: note 74227 added to function.ob-get-status




note 74227 added to function.ob-get-status
user name
2007-03-30 11:45:20
note 74227 deleted from function.ob-get-status by nlopess
user name
2007-03-30 17:08:59
Note Submitter: mightye at gmail dot com 

----

I tried to use this function to break out of arbitrary
output buffering depth in order to send text directly to the
web server.  This is necessary if you want to check client
connectivity with connection_aborted() or
connection_status()  (these methods require that you attempt
to send data to the browser before each time you check their
value in order for the value to be meaningful).

I created an ob_pause() pause function which suspends ALL
output buffering, and returns an array representing the
state that output buffering was in.  Then there is an
ob_resume() function which accepts this state array and as
closely as possible attempts to restore output buffering to
its previous state.

Known issue: If you have an output handler set which is
array($class_instance,'method'), the best I can do with the
data PHP makes available to me is restore it as a static
call against class::method.  Anyone who can tell me how to
get a reference to the actual object which was handling
output please drop me a line, your feedback would be most
welcome.

<?php
/**
 * Pauses all output buffering no matter how deep you are.
 * All output buffering is suspended so that you can write
directly to the
 * web server.  Returns an output buffer stack which must be
passed to
 * ob_resume() in order to restore the previous output
buffer state.
 * author Eric Stevens (http://lotgd.net)
 * return array
 */
function ob_pause() {
	$OB_stack = ob_get_status(true);
	for ($x = count($OB_stack) - 1; $x >= 0; $x--) {
		$content = ob_get_clean();
		// Workaround, empty chunk size represents as 0, should be
null.
		if ($OB_stack[$x]['chunk_size'] == 0)
$OB_stack[$x]['chunk_size'] = null;
		$OB_stack[$x]['content'] = $content;
	} 
	return $OB_stack;
}
/**
 * Restores the output buffer state as closely as possible.
 * Restores the output buffer as closely as possible to the
state it was when
 * ob_pause() was called.  Expects the output stack that
ob_pause() returned.
 * This function fails to correctly restore the full state
if any of the output
 * handler callback functions were actually
array($class,'method') since we have
 * no way to retrieve a reference to $class.  Instead it
will try to restore the
 * callback as a static call to same method on the class
definition.
 * author Eric Stevens (http://lotgd.net)
 * param array $OB_stack The return value from
ob_pause() which you are
 * restoring.
 * return void
 */
function ob_resume($OB_stack) {
	for ($x = 0; $x < count($OB_stack); $x++) {
		// we can't rely on ['type'] to determine if this is the
built in default
		// handler since in some (many?) versions of PHP it is
always 1 (one)
		if ($OB_stack[$x]['name'] == 'default output handler') {
			ob_start();
		} else {
			$handler = explode('::',$OB_stack[$x]['name']);
			if (count($handler) > 1){ // a class callback
				// This is only partially functional, we can only
restore static
				// methods on the object since we do not have any
references 
				// to the original object from here.
				ob_start($handler, $OB_stack[$x]['chunk_size'],
$OB_stack[$x]['del']);
			} else {
				ob_start($handler[0], $OB_stack[$x]['chunk_size'],
$OB_stack[$x]['del']);
			}
		}
		echo $OB_stack[$x]['content'];
	}
}

?>

-- 
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php


[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )