2012-02-03 94 views
0

什麼是最好的解決方案,以阻止在循環中在服務器上主動運行ignore_user_abort? (PHP)如何停止ignore_user_abort一次啓動?

基本上試圖阻止一個無限循環PHP腳本,將不會停止,因爲ignore_user_abort設置和腳本執行..

簡單誇張的例子:

<? 

ignore_user_abort(true); 

set_time_limit(0); 

$test = 1; 

while ($test < 1000000000000000) 

{ 

sleep(1); 
$test = $test+1; 
} 

?> 
+0

一個好綱要在什麼情況?你的意思是阻止它在你的腳本?還是你問如何殺死一個無限循環的PHP腳本,它不會因爲'ignore_user_abort'被打開而停止? – rdlowrey 2012-02-03 20:32:36

+0

也許...'退出'? – DaveRandom 2012-02-03 20:32:37

+0

請提供示出一些示例代碼的更多信息。你能夠做出口();? – Cheery 2012-02-03 20:32:45

回答

2

看來connection_aborted是你在找什麼?

<?php 
ignore_user_abort(TRUE); 
set_time_limit(0); 

while (TRUE) { 
    if(connection_aborted()) { 
     break; 
    } 
    sleep(5); 
} 

// do some stuff now that we know the connection is gone 
?> 

或者你可以檢查connection_status和設定的CONNECTION_ABORTED常數:

if(connection_status() == CONNECTION_ABORTED) 
{ 
    break; 
} 

你可以在manual docs on Connection handling

+0

不要以爲這會工作,因爲它已經在運行 – Webby 2012-02-03 21:32:46

+0

我不認爲我明白了什麼你問。你想做什麼,一個簡單的休息或退出不能完成? – rdlowrey 2012-02-04 00:45:02

1
//this will stop the script after running for 30 sec 
set_time_limit(30);