萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> PHP 程式設計

phpMyAdmin 安全防護:三種認證模式詳細設定教學

認證模式比較

在 config.inc.php 中設定 $cfg['Servers'][$i]['auth_type']:

模式值機制適用場景安全性
Config'config'直接讀取設定檔帳密,無登入畫面本機開發、受信任內網⭐ 低(帳密寫在檔案)
HTTP'http'瀏覽器原生 Basic Auth 對話框Apache + mod_php⭐⭐ 中
Cookie'cookie'PHP Session + Cookie 表單登入所有環境(含 IIS、Nginx)⭐⭐⭐ 高(推薦)

推薦設定:Cookie 認證

$cfg['Servers'][$i]['auth_type'] = 'cookie';
$cfg['Servers'][$i]['user'] = 'root';          // 預設用戶(可留空)
$cfg['Servers'][$i]['password'] = '';          // 預設密碼(可留空)
$cfg['Servers'][$i]['AllowNoPassword'] = false; // 禁止空密碼登入
$cfg['Servers'][$i]['LoginCookieValidity'] = 1440; // 登入有效期(秒),預設 1440=24hr
$cfg['Servers'][$i]['LoginCookieStore'] = 0;   // 0=瀏覽器關閉失效,1=永久記住
$cfg['Servers'][$i]['AllowThirdPartyFraming'] = false; // 防止被 iframe 嵌入

HTTP 認證限制說明

⚠️ HTTP 認證僅在 Apache + mod_php (DSO) 模式下運作。
若使用 IIS + PHP (CGI/FastCGI)、Nginx + PHP-FPM,HTTP 認證無法運作,必須改用 Cookie 模式。

進階安全加固

  1. 強制 HTTPS:避免帳密明文傳輸
  2. 修改存取路徑:將 phpMyAdmin 目錄改名為難以猜測名稱(如 pma_7x9k2/)
  3. IP 白名單:Web Server 層面限制只允許特定 IP 存取
  4. 雙因子驗證:搭配 Google Authenticator 外掛
  5. 禁用 Root 遠端登入:MySQL 建立專用帳號、最小權限

常見問題

  • 登入後立即登出:檢查 session.save_path 目錄權限、session.gc_maxlifetime 設定
  • 無法登入:確認 config.inc.php 中 $cfg['Servers'][$i]['host']、port、socket 正確
  • Cookie 模式需啟用 Cookie:瀏覽器不可封鎖 Cookie
copyright © 萬盛學電腦網 all rights reserved