ignore_user_abort 設(shè)置與客戶機(jī)斷開是否會(huì)終止腳本的執(zhí)行。
本函數(shù)返回 user-abort 設(shè)置的之前的值(一個(gè)布爾值)。
int ignore_user_abort ([ string $value ] )
參數(shù) | 描述 |
setting | 可選。如果設(shè)置為 true,則忽略與用戶的斷開,如果設(shè)置為 false,會(huì)導(dǎo)致腳本停止運(yùn)行。 如果未設(shè)置該參數(shù),會(huì)返回當(dāng)前的設(shè)置。 |
PHP內(nèi)置函數(shù)研究系列第二期,采用PHP函數(shù)ignore_user_abort實(shí)現(xiàn)計(jì)劃任務(wù)與持續(xù)進(jìn)程實(shí)例,并通過(guò)一個(gè)可檢測(cè)效果的實(shí)例討論ignore_user_abort()函數(shù)的作用與用法。例-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è)置與客戶機(jī)斷開是否會(huì)終止腳本的執(zhí)行。
一,函數(shù)原型
二,版本兼容
int
ignore_user_abort ( [
bool
setting] )
<?php
ignore_user_abort();
?>
說(shuō)明:調(diào)用ignore_user_abort()函數(shù)聲明即使客戶機(jī)斷開不終止腳本的執(zhí)行。
2,結(jié)合set_time_limit()函數(shù)實(shí)現(xiàn)一個(gè)循環(huán)腳本執(zhí)行任務(wù)
<?php
ignore_user_abort();
set_time_limit(0);
$interval=60*15;
do
{
//執(zhí)行的業(yè)務(wù)
}
while
(
true
);
?>
說(shuō)明:每隔15分鐘循環(huán)執(zhí)行
3,自定義實(shí)現(xiàn)文件輸出并跟蹤ignore_user_abort()函數(shù)的執(zhí)行結(jié)果
<?php
ignore_user_abort ( TRUE );
set_time_limit ( 0 );
$interval = 10;
$stop = 1;
do
{
if
( $stop == 10 )
break
;
file_put_contents(
'liuhui.php'
,
' Current Time: '
.time().
' Stop: '
.$stop);
$stop++;
sleep ( $interval );
}
while
(
true
);
?>
打開liuhui.php文件,文件內(nèi)容如下:
Current Time: 1273735029 Stop: 9
其原理是即使客戶端終止腳本,仍然每隔10秒鐘執(zhí)行一次,并打印出當(dāng)前時(shí)間與終止點(diǎn),這樣就可以測(cè)試出ignore_user_abort()函數(shù)的具體效果。
通過(guò)實(shí)例發(fā)現(xiàn)ignore_user_abort()函數(shù)非常實(shí)用,實(shí)現(xiàn)計(jì)劃任務(wù),完成后續(xù)任務(wù),持續(xù)進(jìn)程等非常有效。更多說(shuō)明請(qǐng)參與PHP手冊(cè)。請(qǐng)關(guān)注下一期PHP內(nèi)置函數(shù)研究系列。
聯(lián)系客服