visitor 填了表格,提交后(我的php程序要进行一些网络查询,执行时间不可定),如果一段时间没看到结果页面,把页面关闭了,但是我不想php程序退出运行,怎么办?
<?php
set_time_limit(10);
sleep(15);
echo "test";
?>结果浏览器一段时间后,显示了 test。难道是 set_time_limit 不起作用? 百度搜:set_time_limit 不起作用,找到很多资料,都说 set_time_limit 在windows下不起作用,只有通过修改 php.ini 里的 max_execution_time 的值 来控制执行时间。可我的空间是 linux 系统。
找到 http://blog.youkuaiyun.com/mengxiangbaidu/article/details/7305536
http://houbolin.cn/blog/2009/12/31/php-set_time_limit-%E5%92%8C-sleep/
Note: The set_time_limit() function and the configuration directive max_execution_time only affect the execution time of thescript itself. Any time spent on activity that happens outside the execution of the script such as system calls using system(), stream operations, database queries, etc. isnot included when determining the maximum time that the script has been running. This is not true on Windows where the measured time is real.
<?php
set_time_limit(40);
while (1) { echo "1"; }
echo "test";
?>Fatal error: Maximum execution time of 40 seconds exceeded inD:\www\2.php on line3
这个显示windows下 set_time_limit 也是起作用的吧。
----------------
测试下 max_execution_time 对 php网络函数的影响
a.php
<?php sleep(50); echo "test2"; ?>b.php
<?php
set_time_limit(10); $url = "http://xxx/a.php";
$ch = curl_init($url); $html = '';
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$html = curl_exec($ch);
curl_close($ch);
echo "test1 ". $html;
?>结果浏览器显示: test1 。说明网络函数在等待数据的时间,是不计入 max_execution_time 的
----------------
a.php
<?php sleep(400); echo "test2"; ?>不是说apache 有个timeout值是300,可是执行a.php后,浏览器还是显示了 test2。 不解。
本文探讨了在PHP中设置时间限制(set_time_limit)及最大执行时间(max_execution_time)的作用,以及如何在不同环境下实现对程序执行时间的有效控制。特别关注了如何避免因网络查询导致的超时问题,并通过实例展示了在Linux系统中使用set_time_limit函数的方法。
3375

被折叠的 条评论
为什么被折叠?



