: This article describes how to implement scheduled tasks in PHP. For more information about PHP tutorials, see. PHP is relatively weak in this regard. if you only use php, you can implement the following:
<? Php ignore_user_abort (); // after closing the browser, run the php code set_time_limit (0); // the program execution time is not limited $ sleep_time = 1; // How long do {$ fp = fopen('test.txt ', 'A +'); fwrite ($ fp, "This is a php blog: phpddt.com \ n "); fclose ($ fp); sleep ($ sleep_time);} while (true);?>
But when I execute the script, even if I close the browser, I cannot stop this program at all, so you need a script execution switch, you can use the method introduced by external files to implement the include switch variable during the while loop. Then we can achieve this:
Create an external variable file switch. php with the following content:
<? Phpreturn 1; // 1 execution script 0 exit execution script?>
The script for improvement is as follows:
<? Php ignore_user_abort (); // after the browser is closed, run the php code set_time_limit (0); // the program execution time is not limited $ sleep_time = 5; // How long does it take to execute $ switch = include 'switch. php '; while ($ switch) {$ switch = include' switch. php'; $ fp = fopen('test.txt ', 'A +'); fwrite ($ fp, "This is a php blog: phpddt.com $ switch \ n"); fclose ($ fp ); sleep ($ sleep_time);} exit ();?>
This script is only feasible for testing and should be less efficient. for lamp, you can use crontab.
Add a small piece of code:
Ignore_user_abort (); // close the browser and run the PHP script. set_time_limit (0); // use set_time_limit (0) to allow unlimited execution of the program $ interval = 60*30; // run do every half an hour {// Here is the code you want to execute sleep ($ interval); // wait 5 minutes} while (true );
So many articles about the implementation of PHP scheduled task execution will be introduced to you later. do not miss out.
The above describes how to implement scheduled tasks in PHP, including some content, and hope to help those who are interested in PHP tutorials.