PHP ob_start 调用了两次,现在无法正确刷新

发布于 2024-12-10 17:15:10 字数 5596 浏览 0 评论 0原文

我编写了一个 MVC 框架,并且使用 ob_start('error_handler') 来捕获致命错误。这太棒了!

在 boot.php

// 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 函数中:

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;

}

同样,这可以很好地处理所有错误!我不使用任何刷新或错误类中的任何内容。

现在,我有一个使用 ob_start 的控制器,因为某种方法的运行时间相当长,我希望向用户反馈正在发生的情况以及脚本的作用。我为此使用 ob_flush() 。

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

为什么?

我如何冲洗的一个例子是这样的: 在控制器方法中:

$this->login->execute();

在 Login::execute 中:

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() 调用都会像这样刷新输出:

public function log( $string )
{

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

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

        ob_flush();
        flush();

    }

}

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

谢谢

::::::::解决方案::::::::::::

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

为它做了一个类:

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

// 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' );

Login::execute():

public function execute( $site = 'mobile' )
{

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

    // Everything in between

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

    // Return $this
    return $this;

}

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

I have written a MVC-framework and I am using ob_start('error_handler') to be able to catch fatal errors aswell. This works great!

In boot.php

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

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;

}

Again, this works fine and dandy to handle all the errors! I don't use any flush or anything inside the Error-class.

Now, I have a Controller that uses ob_start since it's a fairly long running-time of a certain method and I want feedback to the user what's going on and what the script does. I'm using ob_flush() for this.

However, after implementing this ob_start('error_logging'); the ob_flush's don't seem to work. They wait until the entire script is finished(which takes about 8 minutes).

Why?

An example of how I'm flushing is like this:
In Controller-method:

$this->login->execute();

In Login::execute:

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;

}

Each $this->debug->log() call flushes the output like so:

public function log( $string )
{

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

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

        ob_flush();
        flush();

    }

}

Any idea why it's not flushing correctly??
Tell me if you need to view some more code and I'll provide it!!

Thanks

::::::::SOLUTION::::::::::

I had to save the first buffer(the error_logging), end it, start the new one, do my things and then end that one, start the first buffer again and echo the saved output.

Did a class for it:

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 );

    }

}

Usage:

boot.php

// 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' );

Login::execute():

public function execute( $site = 'mobile' )
{

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

    // Everything in between

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

    // Return $this
    return $this;

}

Works perfectly! Won't support several nested ob_start's though since it only saves 1 previous output. Can be fixed though but not neccessary right now!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把时间冻结 2024-12-17 17:15:10

我猜测这与嵌套的 ob_start() 调用有关。我假设您想让它们保持嵌套,但是如果在您的 execute() 函数中您调用 ob_end_flush()ob_end_clean() 在启动下一个输出缓冲区之前?像这样的事情:

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;
}

I'm guessing that it has something to do with the nested ob_start() calls. I'm assuming that you want to keep them nested, but does it flush the way you want it to if in your execute() function you either call ob_end_flush() or ob_end_clean() before you start the next output buffer? Something like this:

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文