How to continue process after responding to ajax request in PHP?

The ignore_user_abort directive, and ignore_user_abort function are probably what you are looking for : it should allow you to send the response to the browser, and, after that, still run some calculations on your server This article about it might interest you : How to Use ignore_user_abort() to Do Processing Out of Band quoting : EDIT 2010-03-22 : removed the link (was pointing to ! Waynepan. Com/2007/10/11/!

How-to-use-ignore_user_abort-to-do-process-out-of-band remove the spaces and! If you want to try ) after seeing the comment of @Joel Basically, when you use ignore_user_abort(true) in your php script, the script will continue running even if the user pressed the esc or stop on his browser. How do you use this?

One use would be to return content to the user and allow the connection to be closed while processing things that don’t require user interaction The following example sends out $response to the user, closing the connection (making the browser’s spinner/loading bar stop), and then executes do_function_that_takes_five_mins() And the given example : ignore_user_abort(true); header("Connection: close"); header("Content-Length: " . Mb_strlen($response)); echo $response; flush(); do_function_that_takes_five_mins() (There's more I didn't copy-paste) Note that your PHP script still has to fit in the max_execution_time and memory_limit constraints -- which means you shouldn't use this for manipulations that take too much time This will also use one Apache process -- which means you should not have dozens of pages that do that at the same time Still, nice trick to enhance use experience, I suppose ;-).

The ignore_user_abort directive, and ignore_user_abort function are probably what you are looking for : it should allow you to send the response to the browser, and, after that, still run some calculations on your server. This article about it might interest you : How to Use ignore_user_abort() to Do Processing Out of Band ; quoting : EDIT 2010-03-22 : removed the link (was pointing to ! Waynepan.Com/2007/10/11/!

How-to-use-ignore_user_abort-to-do-process-out-of-band/ -- remove the spaces and! If you want to try ), after seeing the comment of @Joel. Basically, when you use ignore_user_abort(true) in your php script, the script will continue running even if the user pressed the esc or stop on his browser.

How do you use this? One use would be to return content to the user and allow the connection to be closed while processing things that don’t require user interaction. The following example sends out $response to the user, closing the connection (making the browser’s spinner/loading bar stop), and then executes do_function_that_takes_five_mins(); And the given example : ignore_user_abort(true); header("Connection: close"); header("Content-Length: " .

Mb_strlen($response)); echo $response; flush(); do_function_that_takes_five_mins(); (There's more I didn't copy-paste) Note that your PHP script still has to fit in the max_execution_time and memory_limit constraints -- which means you shouldn't use this for manipulations that take too much time. This will also use one Apache process -- which means you should not have dozens of pages that do that at the same time. Still, nice trick to enhance use experience, I suppose ;-).

It's not working. You can try this to see it: ignore_user_abort(true); header('Content-Type:text/plain'); header("Connection: close"); echo 'something'; sleep(20); exit(); It still takes 20 seconds to respond. – Mask Sep 26 '09 at 14:21 The link to "How to Use ignore_user_abort() to Do Processing Out of Band" contains malware (when viewed via a referrer, suck as SO).

Looks like the blog owner was hacked... – Joel L Mar 23 '10 at 17:59 @Joel : thanks for the comment ; I've edited my answer to remove the link ;; I've just left the URL, without an hyperlink, and with a warning – Pascal MARTIN Mar 23 '10 at 18:02 actually this works , just don't forget to echo 255 charachters first or IE won't close the connection ... – Ronan Dejhero Apr 4 at 15:09.

Spawn a background process and return background process id so user can check on it later via some secret URL.

Sort of depends on what you're trying to accomplish. In my case, I needed to do a bunch of server-side processing, with only a minimal amount of data being sent back to the browser - summary info really. I was trying to create a message sender - sends out an email to over 250 people, but possibly many more (depends on how many have registered with the system).

The PHP mail handler is quick, but for large numbers, not quick enough, so it was bound to time out. To get around that, I needed to delay the timeout on the server/PHP side, and keep the browser hanging on till all data was summarized and displayed. My solution - a teaser.

Essentially, I gave the user a message stating some initial stats (attempting to send this many emails), created 2 DIV boxes (one for current status info, the second for final summary info), displayed the page footer, started the processing, and when finished, updated the summary info. It goes as follows: Start by collecting your data you're going to process, and get some summary info. In my case, I pulled the list of email addresses, validated them and counted them.

Then display the "attempting" info: echo "Message contents:"; echo "$msgsubject$msgbody "; echo "Attempting " . $num_rows . " email addresses."; Now create some DIVs for status/final info: where $boxwidth is the width you'd like your progress bar to be (explained later) Notice that they are essentially empty - we'll fill them later.

Finally, fill out the rest of the page by displaying the footer of the page (in my case I just "included" the appropriate file). Now, all that is still hanging out in the page buffer, either on the server (if PHP is buffering) or the browser (because it hasn't been told we're done yet), so let's cause it to get pushed and/or displayed using the "ignore" and "flush" from above: ignore_user_abort(true); flush(); Now that the browser is starting to display stuff, we need to give the user something to see, so here's the tease - we'll create a status bar that we'll display in the inner DIV, and a final message for the outer DIV.So, as you loop through your data, periodically (I'll leave "how often" up to you to figure out), output the following: set_time_limit (3); ... (process your data here) ... flush(); This will essentially stack up the little 4x20 progress_red images next to each other, making it appear that a red bar is moving across the screen. In my case, I did this 100 times, based on a percentage of what number I was currently processing out of the total number to process.

The "set_time_limit" will add 3 seconds to the existing limit, so that you know your script won't run into the PHP time limit wall. Adjust the 3 seconds for your application as needed. Even though the page is technically "complete", the javascript code will update the DIV html and our progress bar will "move".

When all done, I then report on how many items were actually processed and add that to the outer DIV html and the page is complete: The browser is happy because it's had all it's qualifications met (end of page syntactially), the user sees somethign happening right up to the end, and the server I tend not to use Javascript if I can help it, but in the case, there was no fancy CSS stuff being done through the Javascript, so it's pretty straightforward and easy to see whats going on and low overhead on the browser site. And it functions pretty smoothly. I don't claim this is perfect, but for a simple, low overhead solution, it works for me without having to create cronjobs, extra queuing mechanisms or secondary processes.

Mask - you need the flush() call to have the response sent to the browser immediately.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions