How to implement self-running in php: first create a PHP sample file; then enter "ignore_user_abort();"; finally pass "do{$fp = fopen('test.php','a'). ..}while(true)..." method can realize automatic execution of tasks.
The operating environment of this article: Windows7 system, PHP7.1 version, DELL G3 computer
How to realize self-running of php?
PHP Automatic Code Execution Method
Regarding how to automatically execute PHP code, we usually need to automatically execute the code when doing scheduled tasks, which is often achieved with the help of the system, such as linux's crontab Or the scheduled schedule of Windows, etc. Now I will share an automatic execution implemented in pure code.
The following is a bunch of PHP execution code used. The efficiency is not very good, but it can keep the task automatically executed.
<?php ignore_user_abort(); // 即使client 断开(如关闭浏览器),PHP 脚本也可以继续执行。 set_time_limit(0); $interval = 60*5; do{ $fp = fopen('test.php','a'); fwrite($fp,'rn'.date('Y-m-d H:i:s',time()).'rn'); fclose($fp); sleep($interval); }while(true) echo 'OK';
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to implement self-running in php. For more information, please follow other related articles on the PHP Chinese website!