gpt4 book ai didi

php - 我的 PHP 脚本的安全性

转载 作者:行者123 更新时间:2023-11-29 13:01:52 24 4
gpt4 key购买 nike

我在刚购买的网站上找到了这个源代码。只是想知道这个脚本是否安全?谁能给我解释一下吗?

<?php

if($_GET['map_loc']) {
$code = $_GET['map_loc'];
$result= mysql_query("SELECT ttc.continent_id, ttc.continent_id, c.name FROM territories_to_continents ttc
INNER JOIN continents c
ON ttc.continent_id = c.continent_id
WHERE ttc.code = '$code'
LIMIT 1;
");
$row = mysql_fetch_array($result);
$mapLoc = $row['name'];
}

?>

最佳答案

它绝对不安全...... mysql_* 很旧,不应该使用。您应该使用 PDO 或 mysqli,在下面的示例中,我展示了如何使用 mysqli 来完成它。

<?php

if(isset($_GET['map_loc'])) {
$code = $_GET['map_loc'];

$query = "SELECT ttc.continent_id, ttc.continent_id, c.name FROM territories_to_continents ttc
INNER JOIN continents c
ON ttc.continent_id = c.continent_id
WHERE ttc.code = ?
LIMIT 1";

if($stmt = $mysqli->prepare($query)){
$stmt->bind_param('s', $code);
$stmt->execute();
$stmt->bind_result($ttc.continent_id1, $ttc.continent_id2, $mapLoc);
$stmt->fetch();
$stmt->free_result();
$stmt->close();
}
}
?>

您绝对应该查看这个著名的问题来寻求帮助:How can I prevent SQL injection in PHP?

关于php - 我的 PHP 脚本的安全性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23227745/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com