note 50757 deleted from ref.exec by googleguy

From: Date: Tue, 22 Oct 2013 10:16:26 +0000
Subject: note 50757 deleted from ref.exec by googleguy
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-196963@lists.php.net to get a copy of this message
Note Submitter: sven at nospam grovestreet dot net ---- Hey, I had this problem: I wanted to kick off a PHP script that would possibly run for a while. This is on a web GUI, so there is the problem of a) the user stopping the browser, and b) reloading or revisiting the page. Since this will live on a hosted box over which I have little control, am unsure of whether CLI or the CGI is installed, and didn't want to add Perl to the mix, I came up with this hack. <? $logfile = dirname(__FILE__)."/mail.log"; if (file_exists ( $logfile )) { if ( (time() - filectime( $logfile ))<10 ){ // file exists and was changed less than 10 secs ago header("Refresh: 2; url=''"); print "process running....<br><br>\n\n"; // the below just inverts the log and prints it $logcontents = file($logfile); foreach (array_reverse($logcontents) as $line) { print $line."<br>\n"; } } else { // file exists but has not been changed in 10 secs print "process finished!<br><br>\n\n"; $logcontents = file($logfile); foreach ($logcontents as $line) { print $line."<br>\n"; } // dump the file so I can run the proces again unlink ($logfile); } } else { echo "\n\n"; echo "startig process...<br><br>\n\n"; ob_flush(); flush(); $log = fopen($logfile, "a") or die("error opening log\n"); // don't kill this process no matter how long it runs set_time_limit(0); // keep running even if the user stops the browser ignore_user_abort(true); // dummy stuff. put the real work here for ($i=0; $i<20; $i++) { $thisline = "iteration $i"; fputs($log, $thisline."\n"); print $thisline."<br>\n"; ob_flush(); flush(); sleep(1); } fclose ($log); } ?> This yields: 1st hit: startig process... iteration 0 iteration 1 iteration 2 .... adding lines as long as the request is not stopped on subsequent hits process running.... iteration 2 iteration 1 iteration 0 and refreshing every 2 secs and finally: process finished! iteration 0 iteration 1 iteration 2 iteration 3 iteration 4 .... rest of log file Seems to be pretty stable... - any comments in email please. sven

« previous php.notes (#196963) next »