set_time_limit 不起作用

mar*_*vin 6 php performance time-limiting

我有一个bigintphp 课程,用于计算大数。除了时间限制外,效果很好。

我设置了时间限制

set_time_limit(900);
Run Code Online (Sandbox Code Playgroud)

在我的 bigint.php 文件中,它在本地主机中工作。但在我的网络主机中,当我尝试计算 999^999 时,它会产生错误

致命错误:/home/vhosts/mysite.com/http/bigint/bigint.php 第 156 行超出了最大执行时间 10 秒

这是我的代码:

public function Multiply_Digit($digit){ //class function of bigint
if($digit==0){$this->str="0";}
else
{
$len=$this->length();
$Result = new bigint("0");
                $carry=0;
                $current;
/*line 156:*/   for ($i = 0; $i < $len; $i++)
                {
                    $current = $this->str[$len-$i-1] * $digit;                    
                    if ($i == 0) { $Result->str = ""+(($current + $carry) % 10); }
                    else{ $Result->str .= ""+(($current + $carry) % 10); }                                        
                    $carry = (($current+$carry) - ($current+$carry)%10)/10;
                }
                $Result->str .= ""+$carry;                
                $Result->str = strrev($Result->str);
                $Result->TrimLeadingZeros();                
                $this->str = $Result->str;//sacma oldu.
                }//else. digit not zero
}//Multiply_Digit()
Run Code Online (Sandbox Code Playgroud)

我尝试放入set_time_limit(900);php 文件的开头和类的构造函数,但没有任何效果。

我以为是会话问题,关闭浏览器并重新打开页面,仍然需要10秒的时间限制。

我在这里做错了什么?

Ser*_*rev 5

“当 PHP 在安全模式下运行时,此功能不起作用。除了关闭安全模式或更改 php.ini 中的时间限制之外,没有其他解决方法。” - http://php.net/manual/en/function.set-time-limit.php

核实。


Kuf*_*Kuf 3

也许你的 apache 配置为在 10 秒后停止执行,你的php.ini是否有以下行:

max_execution_time = 10
Run Code Online (Sandbox Code Playgroud)

如果是这样,要禁用超时(不推荐),请将其更改为:

max_execution_time = 0
Run Code Online (Sandbox Code Playgroud)

或您选择的任何整数限制:

set_time_limit(360);
Run Code Online (Sandbox Code Playgroud)

您可以在php 手册中阅读更多内容。一般来说,将其设置为 0 并不是一个好主意,因为如果等待不存在的资源,服务器进程可能会卡住。

更新

如果网站是托管的,则取决于托管公司,请询问他们是否可以。

另一种解决方法是使用 ajax 调用,让客户端而不是服务器等待