magic_quotes_gpc 是 PHP 早期(< 5.4)自動對 $_GET/$_POST/$_COOKIE 進行 addslashes() 的功能。已於 PHP 5.4.0 移除,但舊專案遷移或共享主機可能仍啟用,需檢測並正確處理避免雙重跳脫。
<?php
/**
* 統一處理輸入字串:視 magic_quotes 狀態決定是否 addslashes
*
* @param string $value 輸入值
* @param string $type 用途:'text' (SQL字串), 'int' (整數), 'search' (LIKE查詢)
* @return string 可直接用於 SQL 的字串
*/
function sqlString(string $value, string $type = 'text'): string {
// 1. 修正 magic_quotes 雙重跳脫
if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) {
$value = stripslashes($value);
}
// 2. 依用途處理
switch ($type) {
case 'text':
// 單引號跳脫,包裹單引號
return $value !== '' ? "'" . addslashes($value) . "'" : 'NULL';
case 'search':
// LIKE 查詢:跳脫 % _ \ 並包裹 %...%
$escaped = addcslashes($value, '%_\\');
return "'%" . $escaped . "%'";
case 'int':
// 強制整數
return $value !== '' ? (string)intval($value) : '0';
default:
return "'" . addslashes($value) . "'";
}
}
?>
不應依賴 magic_quotes 或手動 addslashes,應使用 PDO/MySQLi 預處理語句:
<?php
// PDO 參數化查詢(推薦)
$stmt = $pdo->prepare("SELECT * FROM users WHERE email = ? AND status = ?");
$stmt->execute([$email, 1]);
$user = $stmt->fetch();
// 批次插入
$stmt = $pdo->prepare("INSERT INTO logs (message, level, created_at) VALUES (?, ?, ?)");
foreach ($logs as $log) {
$stmt->execute([$log['message'], $log['level'], time()]);
}
?>
php.ini 確認 magic_quotes_gpc = Off、magic_quotes_runtime = Off、magic_quotes_sybase = Off。get_magic_quotes_gpc() 檢查與 stripslashes() 呼叫。htmlspecialchars() 防 XSS(與 SQL 跳脫無關)。<?php
// 僅供舊專案過渡期使用,新專案勿採用
class LegacyInput {
public static function get(string $key, mixed $default = null): mixed {
$value = $_GET[$key] ?? $default;
return self::fixMagicQuotes($value);
}
public static function post(string $key, mixed $default = null): mixed {
$value = $_POST[$key] ?? $default;
return self::fixMagicQuotes($value);
}
private static function fixMagicQuotes(mixed $value): mixed {
if (is_string($value) && function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) {
return stripslashes($value);
}
if (is_array($value)) {
return array_map([self::class, 'fixMagicQuotes'], $value);
}
return $value;
}
}
?>
magic_quotes_gpc 是已移除的歷史遺物,現代 PHP (5.4+) 不存在。