表單提交大量欄位(如動態表格、複雜設定頁)時,$_POST、$_REQUEST 只接收到部分資料,後續欄位神秘消失,且無任何錯誤訊息。經排查發現是 PHP 5.3.9+ 引入的 max_input_vars 限制導致。
PHP 在 main/php_variables.c 的 php_auto_globals_create_post() 中處理 POST 資料,呼叫 SAPI 的 sapi_handle_post(),最終進入 rfc1867_post_handler() (multipart/form-data) 或 php_std_post_handler() (application/x-www-form-urlencoded)。
關鍵程式碼片段 (main/rfc1867.c):
/* 變數計數器 */
static int num_vars = 0;
/* 在註冊每個變數前檢查 */
if (++num_vars > PG(max_input_vars)) {
php_error_docref(NULL, E_WARNING,
"Input variables exceeded %ld. "
"To increase the limit change max_input_vars in php.ini.",
PG(max_input_vars));
return; // 直接返回,停止處理後續變數
}
堆疊追蹤驗證:
#0 rfc1867_post_handler() at rfc1867.c:681 <-- 變數計數超限返回
#1 sapi_handle_post() at SAPI.c:185
#2 php_default_treat_data() at php_variables.c:399
#3 mbstr_treat_data() at mb_gpc.c:69
#4 php_auto_globals_create_post() at php_variables.c:741
#5 zend_auto_global_init() at zend_compile.c:6832
#6 zend_hash_apply() at zend_hash.c:641
#7 php_hash_environment() at php_variables.c:702
#8 php_request_startup() at main.c:1671
#9 main() at fpm_main.c:1887
| PHP 版本 | 預設 max_input_vars | 影響超全域變數 |
|---|---|---|
| 5.3.9 - 7.x | 1000 | $_GET, $_POST, $_REQUEST, $_COOKIE, $_FILES |
| 8.0+ | 1000 | 同上,另含 $_ENV (若啟用) |
注意:max_input_vars 計算的是「變數個數」,而非資料大小。一個 items[0][name]=foo&items[0][price]=100 算 2 個變數。
; php.ini
max_input_vars = 5000
; 同步調大 post_max_size 防止資料截斷
post_max_size = 64M
; 若有大檔上傳
upload_max_filesize = 32M
修改後需重啟 PHP-FPM/Apache:systemctl reload php8.1-fpm
php_value max_input_vars 5000
php_value post_max_size 64M
; 專案根目錄建立 .user.ini
max_input_vars = 5000
post_max_size = 64M
生效需等待 user_ini.cache_ttl (預設 300 秒) 或重啟 PHP-FPM。
<?php
// ❌ 無效!max_input_vars 屬於 PHP_INI_PERDIR,不可在 ini_set() 修改
ini_set('max_input_vars', 5000);
?>
從架構層面減少 POST 變數才是治本之道:
<!-- 原本:每欄位一個 input,N 行 × M 欄 = N×M 個變數 -->
<input name="items[0][name]">
<input name="items[0][price]">
<input name="items[1][name]">
<input name="items[1][price]">...
<!-- 優化:單一 JSON 欄位 -->
<input type="hidden" name="items_json" value='[{"name":"A","price":100},{"name":"B","price":200}]'>
PHP 端:$items = json_decode($_POST['items_json'], true);
大型表單採用「分步驟精靈」或「自動儲存草稿」模式,每次提交 < 500 個變數。
// 前端 fetch API
fetch('/api/items/batch', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(items)
});
// PHP 端統一處理
$input = json_decode(file_get_contents('php://input'), true);
// 完全繞過 max_input_vars 限制
<?php
// test_max_input_vars.php
echo "max_input_vars: " . ini_get('max_input_vars') . "\n";
echo "post_max_size: " . ini_get('post_max_size') . "\n";
// 模擬大量輸入測試
$count = 2000;
$fields = [];
for ($i = 0; $i < $count; $i++) {
$fields["field_$i"] = "value_$i";
}
$postData = http_build_query($fields);
// 使用 cURL 模擬 POST
$ch = curl_init('https://localhost/test_receive.php');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postData,
CURLOPT_RETURNTRANSFER => true,
]);
$response = curl_exec($ch);
curl_close($ch);
echo "接收到變數數: " . $response;
?>
<?php
// test_receive.php
echo count($_POST);
?>
| 指令 | 預設 | 建議值 | 說明 |
|---|---|---|---|
| max_input_vars | 1000 | 3000-10000 | 輸入變數最大個數 |
| max_input_nesting_level | 64 | 64 | 陣列巢狀深度限制 |
| post_max_size | 8M | 64M | POST 資料總大小上限 |
| upload_max_filesize | 2M | 32M | 單檔上傳上限 |
| memory_limit | 128M | 256M+ | 腳本記憶體上限,需 > post_max_size |
| max_execution_time | 30 | 300 | 大量資料處理需更長時間 |
| max_input_time | 60 | 300 | 解析輸入資料最長時間 |
max_input_vars 是 PHP 為防止哈希碰撞 DoS 攻擊 (CVE-2011-4885) 引入的安全機制。調大雖能解決眼前問題,但根治方案是前端改用 JSON 傳輸或分批提交。生產環境建議:php.ini 設定 3000-5000 作為緩衝,同時推動 API 化改造,徹底擺脫表單變數數量限制。