PHP 預設對未定義陣列鍵或變數發出 Notice 級別提示,非致命錯誤。生產環境通常關閉顯示,但開發階段需妥善處理避免邏輯漏洞。
; 開發環境建議 error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED ; 生產環境 error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED display_errors = Off log_errors = On
修改後需重啟 Web 伺服器生效。
$act = isset($_POST['action']) ? $_POST['action'] : '';
if (isset($_POST['action'])) { $act = $_POST['action']; }
if (!empty($_POST['action'])) { $act = $_POST['action']; }
$username = @$_POST['username'];
if (@$_GET['action'] == 'save') { ... }
僅隱藏提示,不解決邏輯問題,除錯困難。
function _get($key, $default = null) {
return isset($_GET[$key]) ? $_GET[$key] : $default;
}
function _post($key, $default = null) {
return isset($_POST[$key]) ? $_POST[$key] : $default;
}
// 使用
$act = _post('action');
$id = _get('id', 0);
@ 抑制,掩蓋潛在 BugUndefined index:陣列鍵不存在($_GET、$_POST、$_SESSION 等)Undefined variable:變數未定義即使用Undefined offset:數值索引陣列越界