函數-ignore_user_abort,這個函數可以幫助我們實現像linux中的cron一樣實現計劃任務,下面一起來看下該如何來實現。
首先看下php手冊對這個函數的解釋
Description
int ignore_user_abort ([ bool $setting ] )
Sets whether a client disconnect should cause a script to be aborted.
也就是說無論客戶端是否關閉瀏覽器,下面的程序都會執行.
再看下其參數
Parameters
setting
If not set, the function will only return the current setting.
這個函數接受一個參數,來決定是否啓用ignore_user_abort的功能。
再看其返回值:
Return Values
Returns the previous setting, as a boolean.
這裏說返回前一次的設置,並且是bool值得,經過我的測試,這個說法是不對的,返回的明明是int型的,不相信的話大家可以寫一個php文件來測試下。
說了這麼多了,到底該如何用php的這個函數實現計劃任務呢?還得藉助另外一個函數,這個函數是set_time_limit,通過 set_time_limit(0)可以設置程序的執行時間爲無限制,php默認的執行時間是30秒,通過set_time_limit(0)可以讓程序無 限制的執行下去。在程序執行之前加上ignore_user_abort(1)和set_time_limit(0) 即可以了,最終程序該如何寫呢?給大 家一個例子。
- 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);
- ?>