tags:

views:

62

answers:

2

If a user clicks a button that will make an ajax post call to a php file, then navigates away from the website or closes the window, will the php file run completely until it finishes?

i want the file to download stuff to my server and post a bunch of information into a mysql database. This could take a minute or two. But i want the task to finish completely no matter what the user does.

if the ajax post/request gets sent, will the file run through completely?

thanks for any info.

+6  A: 

This should help:

ignore_user_abort(true);
set_time_limit(0);       // number of seconds (0 infinity)

Check out the documentation for those two functions for more insight. In general, you shouldn't have a problem, as long as you don't run up against the time limit. This should have all the info you need:

http://us3.php.net/manual/en/features.connection-handling.php

konforce
You should remove the `set_time_limit(0);` as this could lead to forever hanging processes. OP states the script should only take a minute or two so the timeout shouldn't be set very much higher than that.
webbiedave
Agreed, it should be set to a reasonable value that allows the scripts to finish.
konforce
thanks for the answer, this definitely works when the user starts loading the page and then closes the window or stops it from running. it still goes on to load all the data into my mysql database. However, so far it does not seem to work when i make an ajax post request from a different page, then close that page. I;m using jquery $post() for the ajax call. I could be mistaken though, i need to run a few more tests. thanks:-)
benjaminlotan
A: 

this helped me understand:

http://www.php.net/manual/en/features.connection-handling.php

hugo_leonardo
@hugo_leonardo: making synchronous requests will not prevent the user to quit. *Nothing* can prevent the user to stop any request in the middle.
MainMa
yeah, i know. maybe i used the wrong words (assure). editing...
hugo_leonardo
This will also not stop the PHP script from aborting, as Apache and PHP will kill the script as soon as the network connection is closed (unless you set `ignore_user_abort(TRUE)` as stated in konforce's answer.
Marc B
okay i give up! editing again :p
hugo_leonardo