set_time_limit 不起作用

本文探讨了在PHP中设置时间限制(set_time_limit)及最大执行时间(max_execution_time)的作用,以及如何在不同环境下实现对程序执行时间的有效控制。特别关注了如何避免因网络查询导致的超时问题,并通过实例展示了在Linux系统中使用set_time_limit函数的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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。 不解。





@set_time_limit(0)是一个PHP函数,用于设置脚本的最大执行时间为无限长。\[1\]这意味着脚本将不会因为执行时间过长而被中断。在引用\[2\]中的例子中,如果将set_time_limit(0)的注释符去除,程序将一直执行直到结束,而不会在设定的时间内中断。这在需要长时间运行的脚本中非常有用,比如需要进行大量数据处理或者长时间的循环操作。在引用\[3\]中的例子中,通过设置set_time_limit(0)来实现脚本的持续运行,并使用flush()和ob_flush()来清除服务器缓冲区,随时输出脚本的返回值。这样可以实现实时输出脚本的执行结果。 #### 引用[.reference_title] - *1* [set_time_limit(0)是什么意思?](https://blog.youkuaiyun.com/lxw1844912514/article/details/100029487)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [set_time_limit(0) 设置程序执行时间的函数](https://blog.youkuaiyun.com/qq_14989227/article/details/78796551)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [PHP set_time_limit(0)函数用法](https://blog.youkuaiyun.com/weixin_30398227/article/details/98414274)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值