![](/img/trans.png)
[英]php :how i can get the unknown id of an username and password in mysql
[英]How can I fix my php being unable to fetch a username and password from a mySQL database?
我正在尝试使用 mySQL 数据库进行简单登录。 截至目前,我能够很好地连接到数据库,但无论出于何种原因,我都无法弄清楚为什么它不接受用户名和密码的数据(不担心散列权现在,这只是练习)。 每次我输入它时,它只会给我错误消息,并且我正在尝试使用密码为“123”的用户“test”,所以我知道我没有拼写错误。 除了我的错误信息之外,它根本没有说其他任何错误。 这是我当前登录的样子:
<?php
//Start PHP session
session_start();
require_once("db_config.php");
function sanitize($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
//Check if the form has been submitted
if(strtoupper($_SERVER['REQUEST_METHOD']) === 'POST') {
if (isset($_POST['pass']) && isset($_POST['user'])) {
//Predefine variables
$validationed = false;
$err_msg = "";
//Username and password has been submitted by the user
//Receive and sanitize the submitted information
$user = sanitize($_POST['user']);
$passwd = sanitize($_POST['pass']);
$user = mysqli_real_escape_string($conn, $user);
$sql = "SELECT * FROM user_logins WHERE username = '$user';";
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));
$row_count=mysqli_num_rows($result);
//If the record is found, check password.
if ($row_count > 0) {
$row = mysqli_fetch_assoc($result);
if (password_verify($passwd, $row['pass'])) {
$validationed = true;
}
}
if ($validationed === false) {
// If the check completes without finding a valid login, set the error message.
$err_msg = "Invalid Login...Please Try Again";
} else {
// redirect to the main page of the website.
header("Location: travel.php");
exit();
}
}
}
else {
session_destroy();
session_unset();
session_start();
}
?>
<body id="LoginForm">
<div class="container">
<div class="login-form">
<div class="main-div">
<div class="panel">
<h1>Login</h1>
<p>Please enter your username and password</p>
</div>
<?php
//If $err_msg is set, display the message
if(isset($err_msg)) {
echo('<div class="alert alert-danger" role="alert">');
echo(' <strong>' . $err_msg . '</strong>');
echo('</div>');
}
?>
<form id="Login" action="login.php" method="post">
<div class="form-group">
<input type="username" class="form-control" name="user" placeholder="Username">
</div>
<div class="form-group">
<input type="password" class="form-control" name="pass" placeholder="Password">
</div>
<button type="submit" class="btn btn-primary" name='submit'>Login</button>
</form>
</div>
</div>
</div>
</body>
我正在尝试从表“user_logins”访问“用户名”和“通行证”。 显然我做错了什么,但我似乎无法挑出来。 如果有人可以查看此内容并告诉我他们是否发现任何问题,那将是一个很大的帮助! 谢谢!
当使用password_verify
比较密码和hash时,您必须确保hash值是使用随附的 function password_hash
创建的。 这两个功能齐头并进。
因此,要么直接比较这两个值,要么将密码以其散列形式存储到数据库中(使用password_hash
)。
if ($passwd == $row['pass']) {
$validationed = true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.