There are some ways to accomplish what you need, one of them very simple and the other would need interaction with the shell.
AJAX
The first and simplest of all, Request the script via AJAX. Being it asynchronous, you do not need to wait for the answer and will not catch the execution (on the part of the browser).
Some care is needed in this case. It is necessary to close the session
so that in a future request, the server is not occupied by a session
open. For this, use session_write_close
.
exec() and/or shell_exec()
You can use the operating system itself to run or schedule the execution of the script. However, it is worth noting that direct access to shell
is dangerous and should be avoided (or used very carefully).
A little more information, you can get here: https://stackoverflow.com/a/4628279/1628790
Threads
PHP has support for threads
and in that case you could use a thread
without waiting for her answer. It is worth noting that in this case I am "theorizing", because I never used in practice a thread
without waiting for the answer (I don’t even know if it’s possible). But there is one more possible solution.
PHP: pthreads
Considerations
Regardless of the choice, your script will have its execution started and, soon after, will be notified to the user that the result will be sent by email (without the script has been executed completely).
Of all the solutions, I would choose the first one. Easy, fast and will work smoothly and with few changes to your code.
UPDATE
It was asked, in the comments, if the user closing the browser would not interrupt the execution of the script in PHP.
In this case, it’s important to understand how PHP handles a request:
- Request made (in this case, the browser);
- Request received (by server);
- Script execution;
- Output/output;
- Termination of the execution.
Another detail is that steps 3 and 4 can be interspersed. PHP does not need to run the entire script to perform the output. Output can be performed while executing a script.
When output is performed, PHP will check if there is an active connection waiting for a response. At this point, a directive called ignore_user_abort
. Her default pattern is false
.
Basically, this directive defines that if a connection is not active, the execution of the script will be stopped. That is, if there is no browser (or any other type of client) waiting for the request, the execution of the script will be stopped.
However, PHP is only aware that the connection does not exist when trying to perform the first output.
PHP: ignore_user_abort - Notes
PHP will not Detect that the user has aborted the Connection until an Attempt is made to send information to the client. Simply using an echo statement does not Guarantee that information is sent, see flush().
In this case, your script will run until some information is sent to the client. As in your case, no information will be sent to the customer, you will not need to worry about it.
If that were a necessity, it would be enough to amend the directive to true
or use the function ignore_user_abort()
I believe you’re interested in competition, right? If so, do a Threads search with php: http://php.net/manual/en/class.thread.php
– Thiago Cunha