国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

php set_time_limit()用法測試詳解

2024-09-04 11:48:16
字體:
來源:轉載
供稿:網友

在php中set_time_limit函數是用來限制頁面執行時間的,如我想把一個php頁面的執行時間定義為5秒就可以set_time_limit(5)了。

一個php腳本通過crontab每5分鐘執行一次,考慮到腳本執行時間會超過5分鐘,特意用set_time_limit(290)來控制腳本在290秒退出,某天突然發現后臺有多個該腳本的進程在執行,也就是說set_time_limit(290)沒有起作用,為了證明,特意使用如下代碼測試。

實例代碼如下:

  1. set_time_limit(5); 
  2. for ($i = 0; $i < 100; $i++) { 
  3.     echo date('Y-m-d H:i:s') . "n"
  4.     sleep(1); 

無論是在web還是CLI下,上述腳本并沒有在5秒鐘后退出,后來加上ini_set(‘max_execution_time’, 5)測試,結果一樣,那是不是說明set_time_limit函數根本就沒有用呢?其實不然,在 http://stackoverflow.com/questions/5874950/set-max-execution-time-in-php-cli 這里找到根源所在,其實是上面的寫法有問題,例如使用下述代碼:

  1. set_time_limit(5); 
  2. for (;;) { 

執行后,大概5秒鐘就可以看到”Fatal error: Maximum execution time of 5 seconds exceeded in”類似這樣的錯誤提示,說明set_time_limit是起作用的,現在在去看看官方文檔(http://www.php.net/manual/en/function.set-time-limit.php)上關于此函數的說明,在Note中寫到:

The set_time_limit() function and the configuration directive max_execution_time only affect the execution time of the script 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. is not included when determining the maximum time that the script has been running. This is not true on Windows where the measured time is real.

實例代碼如下:

  1. <?php  
  2. //set_time_limit(0);  
  3. $i=1500;  
  4. include ("inc/conn.php");  
  5. while($i>0)  
  6. {  
  7. $sql="INSERT INTO php (php)  
  8. VALUES ('$i')";  
  9. if ($conn->execute($sql)===flase)  
  10. {  
  11. //echo "數據插入錯誤".$conn->errormsg();  
  12. }  
  13. else  
  14. {  
  15. $phpid=$conn->Insert_ID();  
  16. echo $i."已經存入數據庫,編號:".$phpid;  
  17. }  
  18. $i--;  
  19. echo "<hr>";  
  20. }  
  21. ?> 

注意:sleep函數暫停的時間也是不計入腳本的執行時間的,所以也是第一個測試失敗的原因,當你的頁面有大量數據時,建議使用set_time_limit()來控制運行時間,默認是30s,所以需要你將執行時間加長點,如 set_time_limit(300),其中將秒數設為0,表示持續運行!

如:set_time_limit(0)表示長時間鏈接運行!

注意:這個函數的運行需要你關閉安全模式,在php.ini中將safe_mode = Off 安全模式設置為Off,否則將會出現下面錯誤:

Warning: set_time_limit() [function.set-time-limit]: Cannot set time limit in safe mode in

再次注意的是:在php.ini可以通過定義max_execution_time來設置PHP頁面的最大執行時間.比如下面:

set_time_limit(900);

這個函數指定了當前所在php腳本的最大執行時間,雖然設定值是900秒,實際上最大執行時間=php.ini里的max_execution_time數值 - 當前腳本已經執行的時間 + 設定值

假如php.ini里的max_execution_time=30,當前腳本已經執行10秒,則:最大執行時間=30-10+900=920秒。

php中設置set_time_limit不起作用的解決方法:

set_time_limit用來設置腳本的超時時間,用法如下:

set_time_limit(秒數); 

規定從該句運行時起程序必須在指定秒數內運行結束, 超時則程序出錯退出.但是有時候設置set_time_limit沒有效果,set_time_limit函數最好是在linux下執行,windows執行可能也無效.

解決方法:修改php.ini里的max_execution_time = 30了,這個默認是30秒,修改為max_execution_time = 300,重新啟動apache服務器,這樣超時設置為300秒就有提示信息了.

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 道真| 吉木乃县| 崇阳县| 荣昌县| 靖边县| 翁牛特旗| 松溪县| 凤山市| 长治县| 炎陵县| 永年县| 安塞县| 保定市| 东港市| 临沂市| 嘉祥县| 同仁县| 天全县| 萨迦县| 修文县| 嵊州市| 迁西县| 汉寿县| 泽州县| 万全县| 伊春市| 隆昌县| 丰台区| 浮山县| 赞皇县| 铜川市| 彩票| 洛隆县| 安达市| 宁波市| 横山县| 平定县| 牙克石市| 高邑县| 盘山县| 河曲县|