摘要:
在 PHP 7.4 及更高版本中, get_magic_quotes_gpc() 函数已被弃用。 This means that code using this function will raise a warning or error.以下是解决此问题的方法。
在 PHP 7.4 及更高版本中,get_magic_quotes_gpc() 函数已被弃用。这意味着使用该函数的代码将会引发警告或错误。以下是解决此问题的方法。

示例
if (get_magic_quotes_gpc()) {
$string = addslashes($string);
}
上述代码在 PHP 7.4 中将引发错误,因为 get_magic_quotes_gpc() 已被弃用。
解决方案
使用 ini_get 替代
可以使用 ini_get('magic_quotes_gpc') 来检查 magic_quotes_gpc 设置是否启用,从而替代 get_magic_quotes_gpc()。
示例:
$magicQuotesEnabled = (bool) ini_get('magic_quotes_gpc');
if ($magicQuotesEnabled) {
$string = addslashes($string);
}
移除魔术引号相关代码
从 PHP 5.4.0 开始,魔术引号功能已被完全移除。因此,最好的做法是从代码中移除所有与魔术引号相关的代码。
示例:
// 不再需要检查 magic_quotes_gpc,直接处理字符串
$string = addslashes($string);
使用参数化查询和输出转义
为了确保代码的安全性,应使用参数化查询与数据库进行交互,并在输出时进行适当的转义。
示例:
// 使用 PDO 进行参数化查询
$stmt = $pdo->prepare("INSERT INTO table (column) VALUES (:value)");
$stmt->bindParam(':value', $value, PDO::PARAM_STR);
$stmt->execute();
// 输出到 HTML 时使用 htmlspecialchars 转义
echo htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
通过这些方法,可以有效地解决 get_magic_quotes_gpc() 被弃用的问题,并提高代码的安全性和兼容性。