PHP: close connection and continue the execution

In HTTP/1.1 definition, it exists a header called “Connection” that, if you define “close”, you are sending that the connection will be closed after completion of the response. The client will know that the response is complete if it know the response length.

The next code calculates the response length, and send the two headers needed to close the connection and continue the execution in the PHP script:

<?php
 
startOutputBuffer();
 
echo ('Text the user will see');
 
flushOutputBuffer();
 
sleep(30);
echo('Text user will never see');
 
function startOutputBuffer()
{
    ob_end_clean();
    ignore_user_abort();
    ob_start();
}
 
function flushOutputBuffer()
{
    $size = ob_get_length();
    header("Connection: close");
    header("Content-Length: $size");
 
    ob_end_flush();
    flush();
}

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos necesarios están marcados *

Puedes usar las siguientes etiquetas y atributos HTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>