php ignore_user_abort
函數(shù)說(shuō)明(PHP 4中,PHP 5中)
ignore_user_abort 設(shè)置與客戶機(jī)斷開(kāi)是否會(huì)終止腳本的執(zhí)行.
本函數(shù)返回 user-abort 設(shè)置的之前的值(一個(gè)布爾值).
函數(shù)定義
int ignore_user_abort ([ string $value ] )
參數(shù) 描述
setting 可選.如果設(shè)置為 true,則忽略與用戶的斷開(kāi),如果設(shè)置為 false,會(huì)導(dǎo)致腳本停止運(yùn)行.
如果未設(shè)置該參數(shù),會(huì)返回當(dāng)前的設(shè)置.
提示注釋
注釋:PHP 不會(huì)檢測(cè)到用戶是否已斷開(kāi)連接,直到嘗試向客戶機(jī)發(fā)送信息為止.簡(jiǎn)單地使用 echo 語(yǔ)句無(wú)法確保信息發(fā)送,參閱 flush() 函數(shù).
實(shí)例說(shuō)明
例-1 一個(gè)的ignore_user_abort()的例子,配合set_time_limit()函數(shù) 和一個(gè)死循環(huán)就可以實(shí)現(xiàn)計(jì)劃任務(wù)功能.
- <?php
- // Ignore user aborts and allow the script
- // to run forever
- ignore_user_abort (true);
- set_time_limit (0);
- echo 'Testing connection handling in PHP' ;
- // Run a pointless loop that sometime
- // hopefully will make us click away from
- // page or click the "Stop" button.
- while(1)
- {
- // Did the connection fail?
- if( connection_status () != CONNECTION_NORMAL )
- {
- break;
- }
- // Sleep for 10 seconds
- sleep (10);
- }
- // If this is reached, then the 'break'
- // was triggered from inside the while loop
- // So here we can log, or perform any other tasks
- // we need without actually being dependent on the
- // browser.
- ?>
實(shí)例 1、
關(guān)閉瀏覽器后,程序能繼續(xù)在后臺(tái)跑,這種情況下需要用到ignore_user_abort()函數(shù);
- ignore_user_abort(true); //設(shè)置客戶端斷開(kāi)連接時(shí)是否中斷腳本的執(zhí)行
- set_time_limit(0);
- $file = '/tmp/ignore_user.txt';
- if(!file_exists($file)) {
- file_put_contents($file);
- }
- if(!$handle = fopen($file,'a+b')){
- echo "not open file :".$file;
- exit;
- }
- $i=0;
- while($i<100) {
- $time = date("Y-m-d H:i:s",time());
- echo $time."/n";
- if(fwrite($handle,$time."/n")===false) {
- echo "not write file:".$file;
- exit;
- }
- echo "write file time:".$time."/n";
- $i++;
- sleep(2);
- }
- fclose($handle);
新聞熱點(diǎn)
疑難解答