Jquery Post / AJAX - 带代码的关闭窗口是否会影响服务器脚本?

To preface, I know this isn't a great question and it will be hard to explain.

I have a PHP script that takes 5-10 minutes to run. I don't want the user to have to wait for it. If I "trigger" the script using jquery ajax, and then the user navigates away from that page or closes the browser (and doesn't wait for the response (if any) which will come much later), will the script still execute fully (assuming there are no errors etc)

Thanks!

Once the server receives the AJAX Request along with the data, it would process it as usual, even if you close the page or the window. If you close the browser window before the server receives the AJAX Request, the processing it not going to happen.

Furthermore, if the AJAX Request is returning any kind of data or displaying messages, it is advised that you leave the window open, so that there is some "Listening" page to the Server's Request Response.

In your PHP script you could call the ignore_user_abort(true) function which would cause the script to run regardless of the user closing the page or not.

You could use the command line if you have access to one:

php /loc/to/file.php

This does not have a timeout and might be faster than port:80 (a browser calling an phpfile)

Or call the main phpfile in another file via a php's exec():

<?php
exec("php /loc/to/file.php > /loc/to/result.txt");
?>

You might want to use shell_exec() The dir of results.txt and the file itself need to be writable. The 'greater than' sign writes the output of the phpfile to result.txt. If the php would echo 123, that would be the contents of result.txt


5 to 10 minutes is a very long time, you might want to check your code for improvements. If you are using a database, add indice (an index) on columns u use allot, that ccan save huge amounts of tim