2012-02-03 99 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);