Heim  >  Artikel  >  Backend-Entwicklung  >  php页面最大执行时间 set_time_limit函数不起作用

php页面最大执行时间 set_time_limit函数不起作用

PHP中文网
PHP中文网Original
2017-03-21 15:03:056689Durchsuche

set_time_limit 不生效或者无效解决方法

<?php
global $begin;
global $end;
$begin = microtime(TRUE);
try {
$dsn = "mysql:host=localhost;dbname=kaixin";
$db = new PDO($dsn, &#39;root&#39;, &#39;111&#39;);
$db->exec("set names &#39;utf8&#39;");//默认编码
$db->setAttribute(PDO_ATTR_ERRMODE, PDO_ERRMODE_EXCEPTION);
for($i=1; $i<5000; $i++){
$sql = "insert into `article` (`title`, `content`, `time`, `author`, `ip`) value (&#39;文章标题".$i."&#39;, &#39;内容".$i."&#39;,
&#39;1227237892&#39;,&#39;kaixin&#39;,&#39;127.0.0.1&#39;)";
$db->exec($sql);
}
$end = microtime(TRUE);
// by bbs.it-home.org
$time = $end-$begin;
echo "执行了".$time."s";
} catch (Exception $e) {
echo "Failed: " . $e->getMessage();
}
?>

执行这段代码,只能插入大概1000条左右的数据,这样页面就不执行了。 测试页面的执行时间大概都是29秒左右,也就是页面的最大执行时间 是30秒。

知道set_time_limit函数,可以设置页面执行时间。

set_time_limit函数用法如下:

本函式用来设定该页最久执行时间。内定值是 30 秒,在 php.ini 中的 max_execution_time 变数设定,若设定为 0 则不限定最久时间。当执行到该函数时,才开

始计算。例如,若内定是 30 秒,而在执行到该函数前已执行了 25 秒,而用本函式改为 20 秒,则该页面最长执行时间为 45 秒。

把这个set_time_limit函数加到了程序里,

发现页面执行时间还是29秒左右,没有起作用。

看到网上有人说,php.ini里的safe_mode如果为on,本函数不执行。查了我的php.in里的safe_mode=off。

试着把set_time_limit(0);加入到for循环里。

<?php
...
for($i=1; $i<5000; $i++){
set_time_limit(0);
$sql = "insert into `article` (`title`, `content`, `time`, `author`, `ip`) value (&#39;文章标题".$i."&#39;, &#39;内容".$i."&#39;,
<?php
set_time_limit(0);
global $begin;
global $end;
$begin = microtime(TRUE);
...
?>
&#39;1227237892&#39;,&#39;kaixin&#39;,&#39;127.0.0.1&#39;)";
$db->exec($sql);
}
...
?>

也没有起作用。最后在网上查到,有人说,“set_time_limit函数最好是在linux下执行,windows执行可能也无效”。彻底对这个函数失去信心 了,估计是因为我是windows系统的原因。

只能修改php.ini里的max_execution_time = 30了。这个默认是30秒,我修改为max_execution_time = 300.重新启动apache服务器。 结果执行时间为140秒左右,5000条数据终于插入了。 看来,windows下这个页面执行时间还是在php.ini里控制,修改max_execution_time。 如果用PDO往MYSQL里面插入数据的话。插入五千条大概140多秒时间,插入1万条大概260多秒时间。

以上就是php页面最大执行时间 set_time_limit函数不起作用 的内容,更多相关内容请关注PHP中文网(www.php.cn)!

相关文章:

php set_time_limit()设置页面执行时间

php set_time_limit(0) 设置程序执行时间的函数

set_time_limit — 设置脚本最大执行时间

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn