首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP ob_start调用了两次,现在不能正确刷新

PHP ob_start调用了两次,现在不能正确刷新
EN

Stack Overflow用户
提问于 2011-10-20 19:58:15
回答 1查看 4.1K关注 0票数 0

我已经编写了一个MVC框架,并且我正在使用ob_start('error_handler')来捕获致命错误。这个很好用!

在boot.php中

代码语言:javascript
复制
// Start the error-logging
ob_start( 'error_logging' );

// Run the request
Request::run( URL::getInstance()->init()->formatURL() );

// Flush and disable output-buffering
ob_end_flush();

error_logging函数:

代码语言:javascript
复制
function error_logging( $output )
{

    // Get the last error
    $trace = error_get_last();
    // Initialize our final array of fields explaining the error
    $final = array();

    // Check if the error does not exist, return the original output unedited
    if( !isset( $trace ) ) return $output;

    // Loop through or error and build our array
    foreach( $trace as $info => $value ) $final[$info] = $value;

    // Initialize our Error-class
    $error = Error::factory();
    // Handle the error with the fields and build our error-message
    $newOutput = $error->handle( $final['type'] , $final['message'], $final['file'], $final['line'] );

    // Check if the new output is null, if yes set output to the original output, otherwise set it to the new output
    $output = ( $newOutput === null ) ? $output : $newOutput;

    // Return the output
    return $output;

}

同样,这可以很好地处理所有的错误!我没有在Error-class中使用任何flush或任何东西。

现在,我有一个使用ob_start的控制器,因为它是某个方法相当长的运行时间,我希望向用户反馈发生了什么以及脚本做了什么。为此,我使用了ob_flush()。

但是,在实现此ob_start(‘error_logging’)之后,ob_flush似乎不起作用。他们等待整个脚本完成(大约需要8分钟)。

为什么?

我如何刷新的一个示例如下:在Controller-method中:

代码语言:javascript
复制
$this->login->execute();

在Login::execute中:

代码语言:javascript
复制
public function execute( $site = 'mobile' )
{

    ob_start();

    $this->debug->log('--------------------------------------------<br />');
    $this->debug->log( 'Logging into site: ' . ucfirst( $site ) . '<br />' );

    // Initiate cookies
    if( $site == 'full' )
        $this->_initCookies();

    // Set up the URL
    $url = Config::get( $site ) . Config::get( 'url_login' );
    curl_setopt( $this->curl->get(), CURLOPT_URL, $url );

    // CURL-values
    curl_setopt( $this->curl->get(), CURLOPT_FOLLOWLOCATION, 1  );
    curl_setopt( $this->curl->get(), CURLOPT_RETURNTRANSFER, 1  );
    curl_setopt( $this->curl->get(), CURLOPT_POST,           1  );
    curl_setopt( $this->curl->get(), CURLOPT_POSTFIELDS,     Config::get( 'postfields' ) );
    curl_setopt( $this->curl->get(), CURLOPT_COOKIEFILE,     'resources/tmp/cookies.txt');
    curl_setopt( $this->curl->get(), CURLOPT_CONNECTTIMEOUT, 10 );
    curl_setopt( $this->curl->get(), CURLOPT_TIMEOUT,        40 );
    curl_setopt( $this->curl->get(), CURLOPT_USERAGENT,      'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0.1) Gecko/20100101 Firefox/6.0.1');

    // Get the page returned after we've logged in
    $this->login_html = str_get_html( curl_exec( $this->curl->get() ) );

    // Validate the page returned to see if we successfully logged in
    $this->_validateLogin( $site );

    $this->debug->log('--------------------------------------------<br />');

    ob_end_flush();

    // Return $this
    return $this;

}

每次$this->debug->log()调用都会刷新输出,如下所示:

代码语言:javascript
复制
public function log( $string )
{

    if( $this->active() || $this->logging() )
    {

        echo str_repeat("<!-- AGENT SMITH -->", 500);
        echo $string;

        ob_flush();
        flush();

    }

}

你知道为什么它不能正确刷新吗??如果您需要查看更多代码,请告诉我,我将提供它!!

谢谢

:解决方案:

我必须保存第一个缓冲区( error_logging),结束它,启动新的缓冲区,做我的事情,然后结束那个缓冲区,再次启动第一个缓冲区并回显保存的输出。

为此做了一个类:

代码语言:javascript
复制
class Tanaxia_Buffer
{

    public static $instance = null;

    private $main = null;
    private $previousOutput = "";

    public static function getInstance()
    {

        if( Buffer::$instance === null )
            Buffer::$instance = new Buffer();

        return Buffer::$instance;

    }

    public function set_main( $ob )
    {

        $this->main = $ob;

    }

    public function start( $main = null )
    {

        if( $main !== null )
            $this->set_main( $main );

        ob_start( $this->main );

    }

    public function end( $type = 'clean' )
    {

        switch( $type )
        {

            case 'flush': ob_end_flush(); break;
            case 'clean': ob_end_clean(); break;

        }

    }

    public function start_separate()
    {

        $this->previousOutput = ob_get_contents();
        ob_end_clean();

        ob_start();

    }

    public function end_separate( $type = 'flush' )
    {

        switch( $type )
        {

            case 'flush': ob_end_flush(); break;
            case 'clean': ob_end_clean(); break;

        }

        ob_start( $this->main );
        echo $this->previousOutput;

        empty( $this->previousOutput );

    }

}

用法:

boot.php

代码语言:javascript
复制
// Start the error-logging
Buffer::getInstance()->start( 'error_logging' );

// Run the request
Request::run( URL::getInstance()->init()->formatURL() );

// Flush and disable output-buffering
Buffer::getInstance()->end( 'flush' );

登录::execute():

代码语言:javascript
复制
public function execute( $site = 'mobile' )
{

    Buffer::getInstance()->start_separate();

    // Everything in between

    Buffer::getInstance()->end_separate();

    // Return $this
    return $this;

}

完美的工作!不支持多个嵌套的ob_start,因为它只保存一个先前的输出。虽然可以修复,但现在不是必须的!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-10-20 21:49:26

我猜这与嵌套的ob_start()调用有关。我假设你想让它们保持嵌套,但是如果在你的execute()函数中,你在开始下一个输出缓冲区之前调用了ob_end_flush()ob_end_clean(),它会按照你想要的方式刷新吗?如下所示:

代码语言:javascript
复制
public function execute( $site = 'mobile' )
{
    // save existing contents of output buffer
    $previousOutput = ob_get_contents();
    // wipe the output buffer itself
    ob_end_clean();
    // start a new output buffer
    ob_start();

    /* SNIP all the code that wouldn't change */

    // flush the function's output buffer
    ob_end_flush();
    // restart the old output buffer
    ob_start("error_logging");
    // take whatever was in the old buffer and make sure it shows up in the new one
    echo $previousOutput;

    return $this;
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7835595

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档