I want to run a PHP script in the background using another PHP script.
I had this in mind:
exec('/usr/bin/php background.php &');
My webhost has only disabled access to exec().
The only other way I can think of is sending a mail() to an email forwarder which is piped to a script, but that's rather a wacky workaround than a solution.
Does anyone know any solution?
You could achieve that by using curl and loading the script as a web resource so it can be executed. If that script needs to be protected from public, you can check if the request came from the same server:
Be sure to make an asynchronous request with curl, so if the script takes a lot of time to execute, doesn't hang the original user request (use
curl_setopt( $handle, CURLOPT_HEADER, false );
So the plan is from the page where you know you need to execute the script, launch an Http request to the script (this is a very basic example of using curl library):
Then in your script, use the above script to protect requests not comming from current server, and do the job.
Updated
As stated in this question: sending a non-blocking HTTP POST request In your script job you could send this header to close the connection as script is running:
Updated II
Reviewing my own answer, I've checked that
curl_setopt( $handle, CURLOPT_HEADER, false );
does not create an asynchronous request. I still have not found how to do it.I haven't tested this, but if you are able to use curl, you could possibly create background.php with
ignore_user_abort()
and curl it from your main script with a low timeout. The script the user sees will be delayed slightly because it has to wait for the curl request to timeout, but your background should continue working.