The following code, under the current folder, can generate a test.txt and write a timestamp every 20 seconds, regardless of whether the client closes the browser.
Ignore_user_abort (TRUE);
Set_time_limit (0);
function Write_txt () {
if (!file_exists ("Test.txt")) {
$fp = fopen ("Test.txt", "WB");
Fclose ($FP);
}
$str = file_get_contents (' test.txt ');
$str. = "\ r \ n". Date ("H:i:s");
$fp = fopen ("Test.txt", "WB");
Fwrite ($fp, $STR);
Fclose ($FP);
}
function Do_cron () {
Usleep (20000000);
Write_txt ();
}
while (1) {
Do_cron ();
}
The two key functions:
Ignore_user_abort (TRUE), the function is that the following code will be executed regardless of whether the client closes the browser or not.
Set_time_limit (0), the function is to cancel the php file execution time, if there is no such function, the default PHP execution time is 30 seconds, that is, 30 seconds later, the file is say Goodbay.
The Usleep also supports the Windows operating system after PHP5.0.