fastcgi_finish_request

(PHP 5 >= 5.3.3)

fastcgi_finish_requestFlushes all response data to the client

Описание

boolean fastcgi_finish_request ( void )

This function flushes all response data to the client and finishes the request. This allows for time consuming tasks to be performed without leaving the connection to the client open.

Возвращаемые значения

Возвращает TRUE в случае успешного завершения или FALSE в случае возникновения ошибки.

Коментарии

Автор:
There are some pitfalls  you should be aware of when using this function.

The script will still occupy a FPM process after fastcgi_finish_request(). So using it excessively for long running tasks may occupy all your FPM threads up to pm.max_children. This will lead to gateway errors on the webserver.

Another important thing is session handling. Sessions are locked as long as they're active (see the documentation for session_write_close()). This means subsequent requests will block until the session is closed.

You should therefore call session_write_close() as soon as possible (even before fastcgi_finish_request()) to allow subsequent requests and a good user experience.

This also applies for all other locking techniques as flock or database locks for example. As long as a lock is active subsequent requests might bock.
2016-08-04 12:29:31
http://php5.kiev.ua/manual/ru/function.fastcgi-finish-request.html
Here goes a workaround if you can't use fast cgi.
You'll need to replace echo with puts function everywhere:
<?php
   
function puts(){
        static 
$init    false;
       
$numargs func_num_args();
       
$arg_list func_get_args();
       
$string '';
       
        if(
$numargs === 0)
            return;
        else if(
$numargs 1)
           
$string implode(''$arg_list);
        else
           
$string $arg_list[0];
   
        if(
$init === false)
        {
           
// buffer all upcoming output - make sure we care about compression: 
           
if(ob_start("ob_gzhandler")){
               
connsettings('compression'true);
                echo 
'Yes compression';
            }else{
               
ob_start();
                echo 
'No compression<br>';
            }
           
$init true;
           
register_shutdown_function('puts'nulltrue);
        }
        echo 
$string;
    }
    function 
connsettings(/*$name, (optional)$val=$arg_list[1]*/){
        static 
$settings= array();
       
$numargs        func_num_args();
       
$arg_list        func_get_args();
       
$name            $arg_list[0];
       
        if(
$numargs === 1)
            return 
$settings[$name] ?? null;
       
       
$oldVal            $settings[$name] ?? null;
       
$settings[$name]= $arg_list[1];
   
        return 
$oldVal;
    };
    function 
connection_close(){
        if(!
headers_sent())//it may work without this verification when no compression but may lead to uncomplete data
       
{
           
set_time_limit(0);
           
ignore_user_abort(true);
           
// send headers to tell the browser to close the connection
           
ob_end_flush(); // Order here matter, it won't work if it goes after Content-Length
           
if(connsettings('compression') === false)
               
header("Content-Encoding: none");
           
header('Content-Length: ' ob_get_length()); 
           
header('Connection: close');   
           
// flush all output 
           
if(ob_get_level() > 0)
               
ob_flush(); 
           
flush();
        }
    }
//Usage
puts('Whatever you want to print...<br>');
puts('Some more<br>');
connection_close();

puts("It's never gonna be seen<br>");
echo 
'Neither this';
?>
2019-09-12 11:13:35
http://php5.kiev.ua/manual/ru/function.fastcgi-finish-request.html

    Поддержать сайт на родительском проекте КГБ