How do I close a connection early?

I'm trying to do an AJAX call (using JQuery) that will certainly launch a rather lengthy procedure. I would certainly such as the manuscript to merely send a feedback showing that the procedure has actually begun, yet JQuery will not return the feedback till the PHP manuscript is done running.

I've attempted this with a "close" header (listed below), as well as additionally with result buffering ; neither appears to function. Any kind of hunches? or is this something I require to do in JQuery?


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.' );

2022-07-22 18:17:06
Source Share
Answers: 6

Ok, so primarily the means jQuery does the XHR demand, also the ob_flush method will certainly not function due to the fact that you are incapable to run a function on each onreadystatechange. jQuery checks the state, after that picks the correct activities to take (full, mistake, success, timeout). And also although I was incapable to locate a reference, I remember listening to that this does not collaborate with all XHR executions. A method that I think need to benefit you is a cross in between the ob_flush and also for life - structure ballot.

 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');");

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

And due to the fact that the manuscripts are implemented inline, as the barriers are purged, you get implementation. To make this valuable, transform the console.log to a callback method specified in you major manuscript arrangement to receive information and also act upon it. Hope this aids. Thanks, Morgan.

2022-07-24 09:02:00

A different remedy is to add the work to a line up and also make a cron manuscript which look for new work and also runs them.

I needed to do it in this way lately to prevent restrictions enforced by a common host - exec() et alia was impaired for PHP run by the webserver yet can run in a covering manuscript.

2022-07-24 08:27:12

The adhering to PHP hand-operated web page (incl. customer - notes) recommends numerous guidelines on just how to close the TCP link to the internet browser without finishing the PHP script:

Supposedly it calls for a little bit greater than sending out a close header.

OP after that validates: yup, this sufficed: pointing to user-note #71172 (Nov 2006) replicated here:

Closing the customers internet browser link whilst maintaining your php manuscript operating has actually been a concern given that [PHP] 4.1, when the practices of register_shutdown_function() was changed to make sure that it would certainly not instantly close the customers link.

sts at mail dot xubion dot hu Posted the initial solution:

header("Connection: close");
$size = ob_get_length();
header("Content-Length: $size");
error_log("do something in the background");

Which functions penalty till you replace phpinfo() for echo('text I want user to see'); in which instance the headers are never ever sent out!

The remedy is to clearly switch off result buffering and also clear the barrier before sending your header details. Example:

header("Connection: close");
ignore_user_abort(true); // just to be safe
echo('Text the user will see');
$size = ob_get_length();
header("Content-Length: $size");
ob_end_flush(); // Strange behaviour, will not work
flush(); // Unless both are called !
// Do processing here 
echo('Text user will never see');

Just invested 3 hrs attempting to figure this set out, wish it aids a person

Tested in:

  • IE 7.5730.11
  • Mozilla Firefox 1.81

Later on in July 2010 in a related answer Arctic Fire after that connected 2 more customer - notes that were - adhere to - approximately the one above:

2022-07-24 08:06:38

You can attempt to do multithreading.

you can work up a manuscript that makes a system call (making use of shell_exec) that calls the php binary with the manuscript to do your job as the parameter. Yet I do not assume that is one of the most safe and secure means. Possibly you can thighten things up by chrooting the php procedure and also various other things

Alternatively, there is a class at phpclasses that do that Yet I do not recognize the specifics of the execution

2022-07-24 03:42:12

Your trouble can be addressed by doing some identical shows in php. I asked an inquiry concerning it a couple of weeks ago below: How can one use multi threading in PHP applications

And obtained wonderful solutions. I suched as one specifically significantly. The author made a reference to the which can in fact address your trouble quite possibly as I have actually utilized it currently to manage a comparable trouble that showed up a number of days earlier.

2022-07-24 03:25:35

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 > 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 15 times, which will take about 15 seconds.

Make it executable.

chmod 777 bgping

Put this in a file called bgtest.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
    [0] => starting bgping
    [1] => ending bgping

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!

2022-07-24 03:05:59