PHP regularly runs the following code. a test.txt file can be generated under the current folder, and a timestamp can be written to it 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 ();
}
Two key functions:
Ignore_user_abort (true). The function is used to execute the following code regardless of whether the browser is closed on the client.
Set_time_limit (0). The function is used to cancel the execution time of the php file. if this function is not available, the default php execution time is 30 seconds, that is, 30 seconds later, this file says goodbay.
In addition, usleep supports the windows operating system after PHP5.0.