<?php ignore_user_abort(); // run script in background set_time_limit(0); // run script forever $interval=60*15; // do every 15 minutes... do{ // add the script that has to be ran every 15 minutes here // ... sleep($interval); // wait 15 minutes }while(true); ?> <?php ignore_user_abort(); // 即使Client斷開(如關掉瀏覽器),PHP腳本也可以繼續執行. set_time_limit(0); // 執行時間為無限制,php默認的執行時間是30秒,通過set_time_limit(0)可以讓程序無限制的執行下去 $interval=20; // 時間間隔 單位 秒 $key_file="key.txt"; // 配置文件 if (isset($_GET['s'])) { if ($_GET['s']=="0"){ // 停止工作,但不退出 $s="false"; echo "Function is off"; } elseif ($_GET['s']=="1"){ // 工作 $s="true"; echo "Function is on"; } elseif ($_GET['s']=="2"){ // 退出 $s="die"; echo "Function exited"; } else die("Err 0:stop working 1:working 2:exit"); $string = "<?php\n return \"".$s."\";\n?>"; write_inc($key_file,$string,true); exit(); } if(file_exists($key_file)){ do{ $mkey = include $key_file; if ($mkey=="true"){ // 如果工作 ///////////////////// 工作區間 ////////////////////////////////// $showtime=date("Y-m-d H:i:s"); $fp = fopen('func.txt','a'); fwrite($fp,$showtime."\n"); fclose($fp); /////////////////////////////////////////////////////////////////// } elseif ($mkey=="die"){ // 如果退出 die("I am dying!"); } sleep($interval); // 等待$interval分鍾 }while(true); } else die($key_file." doesn't exist !"); function write_inc($path,$strings,$type=false) { $path=dirname(__FILE__)."/".$path; if ($type==false) file_put_contents($path,$strings,FILE_APPEND); else file_put_contents($path,$strings); } ?>