How do I close a connection early?

PhpJqueryAjax

Php Problem Overview


I'm attempting to do an AJAX call (via JQuery) that will initiate a fairly long process. I'd like the script to simply send a response indicating that the process has started, but JQuery won't return the response until the PHP script is done running.

I've tried this with a "close" header (below), and also with output buffering; neither seems to work. Any guesses? or is this something I need to do in JQuery?

<?php

echo( "We'll email you as soon as this is done." );

header( "Connection: Close" );

// do some stuff that will take a while

mail( '[email protected]', "okay I'm done", 'Yup, all done.' );

?>

Php Solutions


Solution 1 - Php

The following PHP manual page (incl. user-notes) suggests multiple instructions on how to close the TCP connection to the browser without ending the PHP script:

Supposedly it requires a bit more than sending a close header.


OP then confirms: yup, this did the trick: pointing to user-note #71172 (Nov 2006) copied here:

> Closing the users browser connection whilst keeping your php script running has been an issue since [PHP] 4.1, when the behaviour of register_shutdown_function() was modified so that it would not automatically close the users connection.

> sts at mail dot xubion dot hu Posted the original solution:

>

> Which works fine until you substitute phpinfo() for echo('text I want user to see'); in which case the headers are never sent!

> The solution is to explicitly turn off output buffering and clear the buffer prior to sending your header information. Example:

>

> Just spent 3 hours trying to figure this one out, hope it helps someone :)

> Tested in:

> - IE 7.5730.11 > - Mozilla Firefox 1.81


Later on in July 2010 in a related answer Arctic Fire then linked two further user-notes that were-follow-ups to the one above:

Solution 2 - Php

It's necessary to send these 2 headers:

Connection: close
Content-Length: n (n = size of output in bytes )

Since you need know the size of your output, you'll need to buffer your output, then flush it to the browser:

// buffer all upcoming output
ob_start();
echo 'We\'ll email you as soon as this is done.';

// get the size of the output
$size = ob_get_length();

// send headers to tell the browser to close the connection
header('Content-Length: '.$size);
header('Connection: close');

// flush all output
ob_end_flush();
ob_flush();
flush();

// if you're using sessions, this prevents subsequent requests
// from hanging while the background process executes
if (session_id()) {session_write_close();}

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

Also, if your web server is using automatic gzip compression on the output (ie. Apache with mod_deflate), this won't work because actual size of the output is changed, and the Content-Length is no longer accurate. Disable gzip compression the particular script.

For more details, visit http://www.zulius.com/how-to/close-browser-connection-continue-execution

Solution 3 - Php

You can use Fast-CGI with PHP-FPM to use the fastcgi_end_request() function. In this way, you can continue to do some processing while the response has already been sent to the client.

You find this in the PHP manual here: FastCGI Process Manager (FPM); But that function specifically is not further documented in the manual. Here the excerpt from the PHP-FPM: PHP FastCGI Process Manager Wiki:


fastcgi_finish_request()

Scope: php function

Category: Optimization

This feature allows you to speed up implementation of some php queries. Acceleration is possible when there are actions in the process of script execution that do not affect server response. For example, saving the session in memcached can occur after the page has been formed and passed to a web server. fastcgi_finish_request() is a php feature, that stops the response output. Web server immediately starts to transfer response "slowly and sadly" to the client, and php at the same time can do a lot of useful things in the context of a query, such as saving the session, converting the downloaded video, handling all kinds of statistics, etc.

fastcgi_finish_request() can invoke executing shutdown function.


Note: fastcgi_finish_request() has a quirk where calls to flush, print, or echo will terminate the script early.

To avoid that issue, you can call ignore_user_abort(true) right before or after the fastcgi_finish_request call:

ignore_user_abort(true);
fastcgi_finish_request();

Solution 4 - Php

Complete version:

ignore_user_abort(true);//avoid apache to kill the php running
ob_start();//start buffer output

echo "show something to user";
session_write_close();//close session file on server side to avoid blocking other requests

header("Content-Encoding: none");//send header to avoid the browser side to take content as gzip format
header("Content-Length: ".ob_get_length());//send length header
header("Connection: close");//or redirect to some url: header('Location: http://www.google.com');
ob_end_flush();flush();//really send content, can't change the order:1.ob buffer to normal buffer, 2.normal buffer to output

//continue do something on server side
ob_start();
sleep(5);//the user won't wait for the 5 seconds
echo 'for diyism';//user can't see this
file_put_contents('/tmp/process.log', ob_get_contents());
ob_end_clean();

Solution 5 - Php

A better solution is to fork a background process. It is fairly straight forward on unix/linux:

<?php
echo "We'll email you as soon as this is done.";
system("php somestuff.php [email protected] >/dev/null &");
?>

You should look at this question for better examples:

PHP execute a background process

Solution 6 - Php

Assuming you have a Linux server and root access, try this. It is the simplest solution I have found.

Create a new directory for the following files and give it full permissions. (We can make it more secure later.)

mkdir test
chmod -R 777 test
cd test

Put this in a file called bgping.

echo starting bgping
ping -c 15 www.google.com > dump.txt &
echo ending bgping

Note the &. The ping command will run in the background while the current process moves on to the echo command. It will ping www.google.com 15 times, which will take about 15 seconds.

Make it executable.

chmod 777 bgping

Put this in a file called bgtest.php.

<?php

echo "start bgtest.php\n";
exec('./bgping', $output, $result)."\n";
echo "output:".print_r($output,true)."\n";
echo "result:".print_r($result,true)."\n";
echo "end bgtest.php\n";

?>

When you request bgtest.php in your browser, you should get the following response quickly, without waiting about 15 seconds for the ping command to complete.

start bgtest.php
output:Array
(
    [0] => starting bgping
    [1] => ending bgping
)

result:0
end bgtest.php

The ping command should now be running on the server. Instead of the ping command, you could run a PHP script:

php -n -f largejob.php > dump.txt &

Hope this helps!

Solution 7 - Php

Here's a modification to Timbo's code that works with gzip compression.

// buffer all upcoming output
if(!ob_start("ob_gzhandler")){
    define('NO_GZ_BUFFER', true);
    ob_start();
}
echo "We'll email you as soon as this is done.";

//Flush here before getting content length if ob_gzhandler was used.
if(!defined('NO_GZ_BUFFER')){
    ob_end_flush();
}

// get the size of the output
$size = ob_get_length();

// send headers to tell the browser to close the connection
header("Content-Length: $size");
header('Connection: close');

// flush all output
ob_end_flush();
ob_flush();
flush();

// if you're using sessions, this prevents subsequent requests
// from hanging while the background process executes
if (session_id()) session_write_close();

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

Solution 8 - Php

I'm on a shared host and fastcgi_finish_request is setup to exit scripts completely. I don't like the connection: close solution either. Using it forces a separate connection for subsequent requests, costing additional server resources. I read the Transfer-Encoding: cunked Wikipedia Article and learned that 0\r\n\r\n terminates a response. I haven't thoroughly tested this across browsers versions and devices, but it works on all 4 of my current browsers.

// Disable automatic compression
// @ini_set('zlib.output_compression', 'Off');
// @ini_set('output_buffering', 'Off');
// @ini_set('output_handler', '');
// @apache_setenv('no-gzip', 1);

// Chunked Transfer-Encoding & Gzip Content-Encoding
function ob_chunked_gzhandler($buffer, $phase) {
	if (!headers_sent()) header('Transfer-Encoding: chunked');
	$buffer = ob_gzhandler($buffer, $phase);
	return dechex(strlen($buffer))."\r\n$buffer\r\n";
}

ob_start('ob_chunked_gzhandler');

// First Chunk
echo "Hello World";
ob_flush();

// Second Chunk
echo ", Grand World";
ob_flush();

ob_end_clean();

// Terminating Chunk
echo "\x30\r\n\r\n";
ob_flush();
flush();

// Post Processing should not be displayed
for($i=0; $i<10; $i++) {
	print("Post-Processing");
	sleep(1);
}

Solution 9 - Php

TL;DR Answer:

ignore_user_abort(true); //Safety measure so that the user doesn't stop the script too early.

$content = 'Hello World!'; //The content that will be sent to the browser.

header('Content-Length: ' . strlen($content)); //The browser will close the connection when the size of the content reaches "Content-Length", in this case, immediately.

ob_start(); //Content past this point...

echo $content;

//...will be sent to the browser (the output buffer gets flushed) when this code executes.
ob_end_flush();
ob_flush();
flush();

if(session_id())
{
	session_write_close(); //Closes writing to the output buffer.
}

//Anything past this point will be ran without involving the browser.

Function Answer:

ignore_user_abort(true);

function sendAndAbort($content)
{
	header('Content-Length: ' . strlen($content));
	
	ob_start();
	
	echo $content;
	
	ob_end_flush();
	ob_flush();
	flush();
}

sendAndAbort('Hello World!');

//Anything past this point will be ran without involving the browser.

Solution 10 - Php

You could try to do multithreading.

you could whip up a script that makes a system call ( using shell_exec ) that calls the php binary with the script to do your work as the parameter. But I don't think that is the most secure way. Maybe you can thighten stuff up by chrooting the php process and other stuff

Alternatively, there's a class at phpclasses that do that <http://www.phpclasses.org/browse/package/3953.html>;. But I don't know the specifics of the implementation

Solution 11 - Php

Joeri Sebrechts' answer is close, but it destroys any existing content that may be buffered before you wish to disconnect. It doesn't call ignore_user_abort properly, allowing the script to terminate prematurely. diyism's answer is good but is not generically applicable. E.g. a person may have greater or fewer output buffers that that answer does not handle, so it may simply not work in your situation and you won't know why.

This function allows you to disconnect any time (as long as headers have not been sent yet) and retains the content you've generated so far. The extra processing time is unlimited by default.

function disconnect_continue_processing($time_limit = null) {
	ignore_user_abort(true);
	session_write_close();
	set_time_limit((int) $time_limit);//defaults to no limit
	while (ob_get_level() > 1) {//only keep the last buffer if nested
		ob_end_flush();
	}
	$last_buffer = ob_get_level();
	$length = $last_buffer ? ob_get_length() : 0;
	header("Content-Length: $length");
	header('Connection: close');
	if ($last_buffer) {
		ob_end_flush();
	}
	flush();
}

If you need extra memory, too, allocate it before calling this function.

Solution 12 - Php

Note for mod_fcgid users (please, use at your own risk).

Quick Solution

The accepted answer of Joeri Sebrechts is indeed functional. However, if you use mod_fcgid you may find that this solution does not work on its own. In other words, when the flush function is called the connection to the client does not get closed.

The FcgidOutputBufferSize configuration parameter of mod_fcgid may be to blame. I have found this tip in:

  1. this reply of Travers Carter and
  2. this blog post of Seumas Mackinnon.

After reading the above, you may come to the conclusion that a quick solution would be to add the line (see "Example Virtual Host" at the end):

FcgidOutputBufferSize 0

in either your Apache configuration file (e.g, httpd.conf), your FCGI configuration file (e.g, fcgid.conf) or in your virtual hosts file (e.g., httpd-vhosts.conf).

> In (1) above, a variable named "OutputBufferSize" is mentioned. This is the old name of the FcgidOutputBufferSize mentioned in (2) (see the upgrade notes in the Apache web page for mod_fcgid).

Details & A Second Solution

The above solution disables the buffering performed by mod_fcgid either for the whole server or for a specific virtual host. This might lead to a performance penalty for your web site. On the other hand, this may well not be the case since PHP performs buffering on its own.

In case you do not wish to disable mod_fcgid's buffering there is another solution... you can force this buffer to flush.

The code below does just that by building on the solution proposed by Joeri Sebrechts:

<?php
    ob_end_clean();
    header("Connection: close");
    ignore_user_abort(true); // just to be safe
    ob_start();
    echo('Text the user will see');
    
    echo(str_repeat(' ', 65537)); // [+] Line added: Fill up mod_fcgi's buffer.
    
    $size = ob_get_length();
    header("Content-Length: $size");
    ob_end_flush(); // Strange behaviour, will not work
    flush(); // Unless both are called !
    // Do processing here 
    sleep(30);
    echo('Text user will never see');
?>

What the added line of code essentially does is fill up mod_fcgi's buffer, thus forcing it to flush. The number "65537" was chosen because the default value of the FcgidOutputBufferSize variable is "65536", as mentioned in the Apache web page for the corresponding directive. Hence, you may need to adjust this value accordingly if another value is set in your environment.

My Environment

  • WampServer 2.5
  • Apache 2.4.9
  • PHP 5.5.19 VC11, x86, Non Thread Safe
  • mod_fcgid/2.3.9
  • Windows 7 Professional x64

Example Virtual Host

<VirtualHost *:80>
    DocumentRoot "d:/wamp/www/example"
    ServerName example.local

	FcgidOutputBufferSize 0

	<Directory "d:/wamp/www/example">
    	Require all granted
	</Directory>
</VirtualHost>

Solution 13 - Php

this worked for me

//avoid apache to kill the php running
ignore_user_abort(true);
//start buffer output
ob_start();

echo "show something to user1";
//close session file on server side to avoid blocking other requests
session_write_close();

//send length header
header("Content-Length: ".ob_get_length());
header("Connection: close");
//really send content, can't change the order:
//1.ob buffer to normal buffer,
//2.normal buffer to output
ob_end_flush();
flush();
//continue do something on server side
ob_start();
//replace it with the background task
sleep(20);

Solution 14 - Php

Your problem can be solved by doing some parallel programming in php. I asked a question about it a few weeks ago here: How can one use multi threading in PHP applications

And got great answers. I liked one in particular very much. The writer made a reference to the Easy Parallel Processing in PHP (Sep 2008; by johnlim) tutorial which can actually solve your problem very well as I have used it already to deal with a similar problem that came up a couple of days ago.

Solution 15 - Php

Ok, so basically the way jQuery does the XHR request, even the ob_flush method will not work because you are unable to run a function on each onreadystatechange. jQuery checks the state, then chooses the proper actions to take (complete,error,success,timeout). And although I was unable to find a reference, I recall hearing that this does not work with all XHR implementations. A method that I believe should work for you is a cross between the ob_flush and forever-frame polling.

<?php
 function wrap($str)
 {
  return "<script>{$str}</script>";
 };

 ob_start(); // begin buffering output
 echo wrap("console.log('test1');");
 ob_flush(); // push current buffer
 flush(); // this flush actually pushed to the browser
 $t = time();
 while($t > (time() - 3)) {} // wait 3 seconds
 echo wrap("console.log('test2');");
?>

<html>
 <body>
  <iframe src="ob.php"></iframe>
 </body>
</html>

And because the scripts are executed inline, as the buffers are flushed, you get execution. To make this useful, change the console.log to a callback method defined in you main script setup to receive data and act on it. Hope this helps. Cheers, Morgan.

Solution 16 - Php

An alternative solution is to add the job to a queue and make a cron script which checks for new jobs and runs them.

I had to do it that way recently to circumvent limits imposed by a shared host - exec() et al was disabled for PHP run by the webserver but could run in a shell script.

Solution 17 - Php

If flush() function does not work. You must set next options in php.ini like:

output_buffering = Off  
zlib.output_compression = Off  

Solution 18 - Php

Latest Working Solution

    // client can see outputs if any
    ignore_user_abort(true);
    ob_start();
    echo "success";
    $buffer_size = ob_get_length();
    session_write_close();
    header("Content-Encoding: none");
    header("Content-Length: $buffer_size");
    header("Connection: close");
    ob_end_flush();
    ob_flush();
    flush();

    sleep(2);
    ob_start();
    // client cannot see the result of code below

Solution 19 - Php

After trying many different solutions from this thread (after none of them worked for me), I've found solution on official PHP.net page:

function sendResponse($response) {
    ob_end_clean();
    header("Connection: close\r\n");
    header("Content-Encoding: none\r\n");
    ignore_user_abort(true);
    ob_start();

    echo $response; // Actual response that will be sent to the user

    $size = ob_get_length();
    header("Content-Length: $size");
    ob_end_flush();
    flush();
    if (ob_get_contents()) {
        ob_end_clean();
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEric_WVGGView Question on Stackoverflow
Solution 1 - PhpJoeri SebrechtsView Answer on Stackoverflow
Solution 2 - PhpTimbo WhiteView Answer on Stackoverflow
Solution 3 - PhpJorrit SchippersView Answer on Stackoverflow
Solution 4 - PhpdiyismView Answer on Stackoverflow
Solution 5 - PhpSalman AView Answer on Stackoverflow
Solution 6 - PhpLiamView Answer on Stackoverflow
Solution 7 - PhpAndrew WinterView Answer on Stackoverflow
Solution 8 - PhpskibulkView Answer on Stackoverflow
Solution 9 - PhpFluorescentGreen5View Answer on Stackoverflow
Solution 10 - PhppaanView Answer on Stackoverflow
Solution 11 - PhpWalfView Answer on Stackoverflow
Solution 12 - PhpTasosView Answer on Stackoverflow
Solution 13 - PhpWilliemView Answer on Stackoverflow
Solution 14 - PhpSteve ObbayiView Answer on Stackoverflow
Solution 15 - PhpMorgan ARR AllenView Answer on Stackoverflow
Solution 16 - PhpOle HelgesenView Answer on Stackoverflow
Solution 17 - PhpNir O.View Answer on Stackoverflow
Solution 18 - PhpSudharshana S L SrihariView Answer on Stackoverflow
Solution 19 - PhpWhiteAngelView Answer on Stackoverflow