gpt4 book ai didi

php - 使用 MySQLi 时如何防止 SQL 注入(inject)?

转载 作者:行者123 更新时间:2023-11-29 02:28:46 25 4
gpt4 key购买 nike

<分区>

我如何在这样的 SQL 查询中防止 SQL 注入(inject)?

<?php
$mysqli = new mysqli("ip", "username", "pass", "database");

/* check connection */
if (mysqli_connect_errno()) {
printf("Sorry, the login server is 'Under Maintainance'");
exit();
}

$username = $_POST["username1"];
$username = strtolower($username);
$password = $_POST['password1'];
$hash = sha1(strtolower($username) . $password);

$query = "SELECT * FROM accounts WHERE name='$username'";

if ($result = $mysqli->query($query)) {
/* determine number of rows result set */
$rownum = $result->num_rows;

if($rownum != 0)
{
while ($row = $result->fetch_assoc()) {
{
$acct = $row['acct'];
$pass = $row['pass'];
}
if($hash == $pass){
session_start();
$_SESSION['name']=$username;
$_SESSION['acct']=$acct;
header('Location:index.php');
} else {
echo 'There was an error when logging in. Make sure your password and username are correct.';
}
}
$result->close();
}
else
{
echo 'Account does not exist. Please <a href="register.php">Register</a> an account before logging in.';
}
$mysqli->close();
}
?>

我已经添加了加密,但我似乎找不到我知道如何使用的预防方法。另外,用户是否可以在不使用输入框的情况下使用 MySQL 注入(inject)? (页面剖析???)

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