CodeIgniter 的 `Input::_clean_input_keys()` 預設使用正則 `/^[a-z0-9:_/-]+$/i` 驗證 `$_GET`、`$_POST`、`$_COOKIE` 鍵名。當鍵名包含特殊字元(如中文、方括號、點號、空格等)時觸發此錯誤。
修改 `system/core/Input.php` 中的 `_clean_input_keys` 方法,改用配置項 `permitted_uri_chars` 並對鍵名進行 `rawurlencode` 後再驗證:
<?php
function _clean_input_keys($str)
{
$config =& get_config('config');
// 關鍵:先 rawurlencode,再用配置的字元集驗證
if (!preg_match('/^[' . $config['permitted_uri_chars'] . ']+$/i', rawurlencode($str))) {
exit('Disallowed Key Characters.');
}
if (UTF8_ENABLED === TRUE) {
$str = $this->uni->clean_string($str);
}
return $str;
}
?>
在 `application/config/config.php` 中放寬 `permitted_uri_chars`,例如:
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-\[\]@\$\!\*\'\\(\\)\/\+,;=&#';
根據實際業務需求添加允許字元,**切勿直接設為 `.*` 導致安全風險**。