0

It's a long running script and needs to be running even when user closed the browser.

<?php
ignore_user_abort(true); //run even when user disconnected.
set_time_limit(0); //run forever if that's what you need :)
$report = "";
while(1){
// do stuff and touch $report
}
//And the line below doesn't work when user disconnected.
file_put_contents("./offline.txt", $report, FILE_APPEND | LOCK_EX);
?>

I have tried register_shutdown_function() but it doesn't seem to work.
Is there any quick fix or other alternative solution which handles the task?

2 Answers2

0

How about creating another script and running it in the background?

You can take a look at the 3rd answer here

Don't forget to put an ampersand (&) at the end of the command so that it will actually be run in the background, e.g.

exec('php newscript.php > /dev/null &');
Community
  • 1
  • 1
mrun
  • 744
  • 6
  • 19
  • 24
0

Maybe you could use crontab to handle the script. You don't specify how you must fire the script - if you run it in reply to user behavior - suggest to use exec but if you run it for example one a day - use crontab

violator667
  • 469
  • 4
  • 19