php - 无论我如何尝试都无法让 PHP 刷新工作

标签 php ajax flush

好吧,我一生都无法弄清楚为什么flush()不起作用。我到处用谷歌搜索这个问题但没有成功。目前我没有 nginx 或 fastCGI(已经提到它们有特殊需求)。我有一个 php.ini 覆盖文件来更改这些值:

output_buffering = Off
implicit_flush = 1
zlib.output_compression = 0

我已经在实际的 php 中尝试了一切,但这就是我目前所拥有的:

ignore_user_abort(true);
set_time_limit(0);
header('Content-Encoding: none;');
header('X-Accel-Buffering: no');
header("Connection: close");
echo 'success?';
ob_flush();
flush();

/******** background process starts here ********/

sleep(2);
echo 'dammit';

这实际上只是测试信息。最终目标是使用 jquery 通过 ajax POST 提交数据并发回成功并继续处理信息。用户不应该等待脚本的其余部分运行。如果我直接调用该文件,我什至无法让它工作!无论我如何工作,两个 echo 都会同时发出!

最佳答案

老天原谅我这段代码:

protected function detachBrowser()
{
    ob_start();

    // tell PHP to ignore if the browsers closes connection
    @ignore_user_abort(true);
    // check it worked 
    $defer = @ignore_user_abort();
    // according to the docs, in some cases on IIS+CGI
    // ignore_user_abort does not work
    // If so, just abort.
    if (!$defer)
    {
        throw new RuntimeException("Webserver does not support ignore_user_abort()");
    }

    // remove the buffer, even nested ones
    while (ob_get_level()) ob_end_clean();

    /* close the frigging connection with the browser, and help IE understand the message */
    ob_start();
    header('Content-Type: text/plain');
    header('Content-Length: 0');
    header("Content-Encoding: none\r\n");
    header('Connection: close');
    // we need all three, in this precise order
    @flush();
    @ob_end_flush();
    @ob_flush();
}

编辑:我从 DokuWiki 派生此代码,第一个为了实际有用的目的(搜索索引)而实现网络错误的 PHP 项目之一。

关于php - 无论我如何尝试都无法让 PHP 刷新工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24398251/

相关文章:

database - 刷新数据库是什么意思?还有 "flash"

Java WebSocket 服务器 OutputStream 不刷新

javascript - Laravel 5.6 更新模型中表单的记录未提交

php - 如何在codeigniter中调用自定义助手的自定义同级方法并使用数据库

php - 使用客户 token 获取订单列表 - Magento 2 Rest Api

javascript - 为什么 Ajax 调用在 WebDriverWait (Selenium) 期间被卡住

php - AJAX + PHP + 下载 mPDF 生成的文件

java - 等价于cin.tie(0)在Java中吗?

php - cURL,curl_multi_init 是多线程还是异步 API?

jquery - ajax失败后重新绑定(bind)当前函数?