请问这段代码安全吗?
/* Create a new mysqli object with database connection parameters */
$mysqli = new mysql('localhost', 'username', 'password', 'db');
if(mysqli_connect_errno()) {
echo "Connection Failed: " . mysqli_connect_errno();
exit();
}
/* Create a prepared statement */
if($stmt = $mysqli -> prepare("SELECT priv FROM testUsers WHERE username=?
AND password=?")) {
/* Bind parameters
s - string, b - boolean, i - int, etc */
$stmt -> bind_param("ss", $user, $pass);
/* Execute it */
$stmt -> execute();
/* Bind results */
$stmt -> bind_results($result);
/* Fetch the value */
$stmt -> fetch();
echo $user . "'s level of priviledges is " . $result;
/* Close statement */
$stmt -> close();
}
/* Close connection */
$mysqli -> close();发布于 2010-12-21 22:07:42
就对mySQL注入的保护而言:是的。Mysqli的参数化查询是安全的,不会受到注入攻击。
如果echo来自外部源,则可能需要添加htmlentities()回显语句,以防止用户使用<script>(some malicious code)</script>等用户名进行注册
发布于 2010-12-21 22:08:54
呼叫本身是安全的。不过,您可能希望将此$mysqli = new mysql('localhost', 'username', 'password', 'db');放在一个单独的文件中,放在您的公共web目录之外。
发布于 2010-12-21 22:48:44
除了Pekka的评论:也在echo语句中使用$result上的htmlspecialchars。
https://stackoverflow.com/questions/4500070
复制相似问题