Answer the question
In order to leave comments, you need to log in
How to run background php?
It is necessary that when the user goes to index.php, the bg.php script is executed in the background, for example:
<?php
sleep(10);
file_put_contents('test.txt', 'success');
?>
Answer the question
In order to leave comments, you need to log in
1) Your version on Ajax, the problem is that the session will be blocked, and you will not go to other pages until the script completes execution, here you can call session_write_close(); but still it is a crutch option, but a working one.
2) If you have Apache, then you can do this:
echo 'ok';
set_time_limit(0);
ignore_user_abort(true);
header("Connection: close");
ob_flush();
flush();
sleep(10);
file_put_contents('test.txt', 'success');
public function isWin(){
return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
}
public function backgroundProcess($command){
if(!$this->isWin()){
$command = $command.' > /dev/null &';
}
$result = exec($command);
return $result;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question