我们正在从 php 5.3 升级到 5.4,它不向后兼容 'get_magic_quotes_gpc'。我知道代码仍然可以工作,但每次都带回一个 FALSE。
但是,我认为是时候从我们的代码中清除它了。
这是一个典型的例子:
$product_id = "0";
if (isset($HTTP_GET_VARS["id"])) {
$rid = (get_magic_quotes_gpc()) ? $HTTP_GET_VARS["id"] : addslashes($HTTP_GET_VARS["id"]);
}
//then $product_id gets used all over the place in many different queries
我一直在研究如何解决这个问题,这就是我想出的:
$rid = "0";
if (isset($HTTP_GET_VARS["id"])) {
$rid = addslashes($HTTP_GET_VARS["id"]);
}
我在这里有点过头了。我知道这一切都与 SQL 注入等有关。我的解决方案是合理/可接受的吗?
提前致谢。
<<<< 编辑 - 附加信息 >>>>
感谢您的回复。实际上,大约 18 个月前,我们进行了一系列转换为 PDO(主要是由于关于 stackoverflow 的这种类型的建议 :)
所以,我可能有一些多余的、毫无意义的代码。这是我在上面发布的从 URL 获取变量的代码下面发生的事情的全貌。
您会看到,曾经存在的 (get_magic_quuotes_gpc) 现在被注释掉并被 (addslashes) 取代。但是该变量被传递给 PDO 查询。
$product_id = "0";
if (isset($HTTP_GET_VARS["id"])) {
//$product_id = (get_magic_quotes_gpc()) ? $HTTP_GET_VARS["id"] : addslashes($HTTP_GET_VARS["id"]);
$product_id = addslashes($HTTP_GET_VARS["id"]);
}
// NEW QUERIES - BEG xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
try {
# MySQL with PDO_MYSQL
$pdo = new PDO("mysql:host=$hostname_db;dbname=$database_db", $username_db, $password_db);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
// Query 1: product details
$stmt = $pdo->prepare('SELECT
[a bunch of stuff here, fields, joins, etc]
WHERE product_id = ? ');
$stmt -> execute(array($rid));
$row_count_resto_details = $stmt->rowCount();
$row_resto_details = $stmt->fetch(PDO::FETCH_ASSOC);
}
// Error message for pdo
catch(PDOException $e) {
echo $e->getMessage();
}
// END QUERY xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
我可以去掉前 4-5 行代码中的所有东西,然后让它变成这样:
$product_id = $HTTP_GET_VARS["id"];