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

首頁 > 網站 > Apache > 正文

win2003下PHP使用preg_match_all導致apache崩潰問題的解決方法

2024-08-27 18:27:38
字體:
來源:轉載
供稿:網友

小編的平臺是windows server 2003(32位系統) + Apache/2.2.9 (Win32) + PHP/5.2.17,在使用正則表達式 preg_match_all (如 preg_match_all("/ni(.*?)wo/", $html, $matches);)進行分析匹配比較長的字符串 $html 時(大于10萬字節,一般用于分析采集回來的網頁源碼),Apache服務器會崩潰自動重啟。
    在Apache錯誤日志里有這樣的提示:
代碼如下:[Thu Apr 11 18:31:31 2013] [notice] Parent: child process exited with status 128 -- Restarting.
[Thu Apr 11 18:31:31 2013] [notice] Apache/2.2.9 (Win32) PHP/5.2.17 configured -- resuming normal operations
[Thu Apr 11 18:31:31 2013] [notice] Server built: Jun 13 2008 04:04:59
[Thu Apr 11 18:31:31 2013] [notice] Parent: Created child process 2964
[Thu Apr 11 18:31:31 2013] [notice] Disabled use of AcceptEx() WinSock2 API
[Thu Apr 11 18:31:31 2013] [notice] Child 2964: Child process is running
[Thu Apr 11 18:31:31 2013] [notice] Child 2964: Acquired the start mutex.
[Thu Apr 11 18:31:31 2013] [notice] Child 2964: Starting 350 worker threads.
[Thu Apr 11 18:31:31 2013] [notice] Child 2964: Listening on port 80.
    經過查閱Apache官方以及論壇資料后,發現win平臺下用正則 preg_match_all 或preg_match 分析比較長的字符串時,導致apache崩潰重啟的原因是windows平臺下默認分配的線程堆棧空間 ThreadStackSize 太小導致的。 win32默認只有256KB,而在 linux下默認值是 8M,這就是為什么同樣的程序在 linux平臺下正常,而在 win平臺下不正常的原因。
    根據PCRE library的官方說明:256 KB 的堆棧空間對應的pcre.recursion_limit大小應該不超過524。
Here is a table of safe values of pcre.recursion_limit for a variety of executable stack sizes:
下面就是一張Stacksize和pcre.recursion_limit對應的建議安全值,超過這個數值就極有可能發生堆棧溢出,apache crash:
代碼如下:Stacksize   pcre.recursion_limit
 64 MB      134217
 32 MB      67108
 16 MB      33554
  8 MB      16777
  4 MB      8388
  2 MB      4194
  1 MB      2097
512 KB      1048
256 KB      524

如果你沒有調整堆棧大小,就必須在使用正則的PHP頁面最開頭加入:
代碼如下:<?php
ini_set("pcre.recursion_limit", "524"); // PHP default is 100,000.
?>

查看具體的錯誤可以使用下面的代碼:
代碼如下:$resultsArray = preg_match_all("/table.*?<a>/isU", $html, $contents);
if ($resultsArray === 0){
echo get_pcre_err();
}
function get_pcre_err(){
        $pcre_err = preg_last_error();  // PHP 5.2 and above.

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 江山市| 静乐县| 灵寿县| 宿州市| 敖汉旗| 中卫市| 普格县| 玛沁县| 博乐市| 呼图壁县| 南平市| 武乡县| 长宁县| 河北区| 东山县| 银川市| 日土县| 天气| 安庆市| 南皮县| 濉溪县| 偏关县| 肇庆市| 洞头县| 固安县| 菏泽市| 来凤县| 札达县| 蕲春县| 潮安县| 工布江达县| 垣曲县| 崇义县| 新竹县| 肇州县| 乐亭县| 潢川县| 科尔| 新绛县| 宿迁市| 恩施市|