Php 重定向但仍继续处理exec()

Php 重定向但仍继续处理exec(),php,redirect,output-buffering,Php,Redirect,Output Buffering,这个代码示例应该重定向并继续处理一个长操作,但是它直到exec命令完成后才重定向。我尝试过多种其他方法,但都没有效果。我哪里做错了 在my php.ini中启用了ignore_user_abort() <?php set_time_limit ( 0 ); header ( 'Connection: close' ); ob_start (); header ( 'Content-Length: 0' ); header ( 'Location: /redirect.php' ); ob_

这个代码示例应该重定向并继续处理一个长操作,但是它直到
exec
命令完成后才重定向。我尝试过多种其他方法,但都没有效果。我哪里做错了

在my php.ini中启用了ignore_user_abort()

<?php
set_time_limit ( 0 );
header ( 'Connection: close' );
ob_start ();
header ( 'Content-Length: 0' );
header ( 'Location: /redirect.php' );
ob_end_flush ();
flush ();
ignore_user_abort ( true );

exec('command that takes 5 minutes to process');
exit ();
?>


我提前感谢您的帮助。

我最近不得不编写一个webhook处理程序,它应该在继续处理其他长时间运行的任务之前立即返回“200OK”响应

我观察到,当整个过程使用同一个web服务器时,立即挂断的过程是不可能的。因此,如果您需要发送响应的连接的客户端IP与服务器IP相同,则您可能永远无法执行此操作。但当连接的传入客户端是真实世界的远程客户端时,它总是起作用

说到这里,我只是回复了一个“200 OK”的回复。这与发送回重定向没有什么不同

对我有效的代码是

public function respondASAP($responseCode = 200)
{
    // check if fastcgi_finish_request is callable
    if (is_callable('fastcgi_finish_request')) {
        /*
         * This works in Nginx but the next approach not
         */
        session_write_close();
        fastcgi_finish_request();

        return;
    }

    ignore_user_abort(true);

    ob_start();
    http_response_code($responseCode);
    header('Content-Encoding: none');
    header('Content-Length: '.ob_get_length());
    header('Connection: close');

    ob_end_flush();
    ob_flush();
    flush();
}

您可以根据自己的情况对其进行调整,使其设置重定向标题,而不是“200 OK”。

您想要什么?您希望重定向发生并且仍然处理下面的代码吗?是的,我希望重定向到另一个页面,并让需要5分钟才能处理的exec()命令继续。不确定这是否是影响它的原因,但您有
ignore\u user\u abort(true)在重定向之后。你确定你有正确的php.ini文件吗?有时会有多个文件。请定义“不工作”好吗?我确信它是正确的php.ini,因为我以前编辑过相同的php.ini,使
上载\u max\u filesize=64M
,并设置
忽略用户\u中止(true)重定向没有修复@Mike它在
exec()之后才重定向已完成