- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在使用 PHP 创建我的第一个网站。我创建了一个 MySQL 数据库,其中包含一个数据库和一个名为“users”的表,其中的列从左到右依次为“Personalid”(自动增量)、“用户名”和“密码”(使用 password_hash($POST_ ['密码'])
。
这是我的以下表单的 HTML 代码
<form method="post" action="idk.php">
<h1>Sign In</h1>
<br>
<input type="text" placeholder="Username" name="username"/>
<input type="password" placeholder="Password" name="password"/>
<button type="submit" name="submit" value="Login">Sign In</button>
</form>
这是我的 php 表单,名为 idk.php
<?php
$conn=mysqli_connect('localhost','root','root',"user_hirsa");
if(!$conn){
die('Could not Connect My Sql:' .mysql_error());
}
else {
echo "Connected";
}
?>
<?php
$stmt = $conn->prepare("SELECT username, password FROM users WHERE username = ?");
$stmt->bind_param('s', $myusername);
$myusername = $_POST['username'];
$mypassword = $_POST['password'];
$hash = $row['password'];
$stmt->execute();
$stmt->bind_result($myusername, $mypassword);
$row = $stmt->fetch(); //fetch DB results
if (!empty($row)) { // checks if the user actually exists(true/false returned)
if (password_verify($mypassword, $hash)) {
echo 'success'; // password_verify success!
} else {
echo 'failed';
}
} else {
echo "This user does not exist"; //username entered does not match any in DB
}
$stmt->close();
$conn->close();
?>
我相信我的问题出在 $hash = $row['password']
上,但我真的不知道。我的结果是数据库连接“已连接”,但始终“失败”,即使我输入正确的密码也是如此。当我输入错误的用户名时,它会显示“该用户不存在”,所以没有问题。
最佳答案
你几乎是正确的。哈希值应该来自数据库。更好的变量命名约定可能有助于发现错误。
我对您的脚本做了一些更改:
<?php
// enable error reporting
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = new mysqli('localhost', 'root', 'root', "user_hirsa");
// you should set the correct charset here
// $conn->set_charset('utf8mb4');
<?php
$myusername = $_POST['username'];
$mypassword = $_POST['password']; // plain password coming from login form
$stmt = $conn->prepare("SELECT password FROM users WHERE username = ?");
$stmt->bind_param('s', $myusername);
$stmt->execute();
// bind columns to variables
$stmt->bind_result($hashInDB);
$row = $stmt->fetch(); //fetch DB results
if ($row) { // checks if the user actually exists(true/false returned)
if (password_verify($mypassword, $hashInDB)) {
echo 'success'; // password_verify success!
} else {
echo 'failed';
}
} else {
echo "This user does not exist"; //username entered does not match any in DB
}
// you don't need these
// $stmt->close();
// $conn->close();
通常,让其他人知道数据库中存在/不存在哪个用户是一个坏主意。只是说凭据不正确。
关于php - 我的登录 PHP 表单不起作用。我相信我的 $hash = $row ['password' ];是问题所在,但我不太确定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58885431/
在我们的服务出现一些预期的增长之后,突然间一些更新花费了非常长的时间,这些过去非常快,直到表达到大约 2MM 记录,现在它们每个需要大约 40-60 秒。 update table1 set fiel
我在服务中实现了一个传感器事件监听器,只要采样周期和最大报告延迟低于 1 秒,该监听器就可以正常工作,但一旦我将采样周期增加到超过 1 秒,传感器就根本不会更新。 我希望采样周期为 10 秒(可能是
我使用 Tkinter GUI 来启动测量和分析过程,基本上只需单击一个按钮即可开始。由于这些测量可能需要一段时间,我尝试添加一个进度条,即这个: http://tkinter.unpythonic.
我正在尝试使用套接字发送数据包,但出现错误。 invalid conversion from ‘omnetpp::cPacket*’ to ‘inet::Packet*’ [-fpermissive]
我刚刚发现 String#split 有以下奇怪的行为: "a\tb c\nd".split => ["a", "b", "c", "d"] "a\tb c\nd".split(' ') => ["a
您好,我正在尝试 ClojureScript,我正在使用 Klipse作为我的 REPL 差不多。这可能不是它的预期用途,但因为我没有做任何太复杂的事情,所以现在没问题。 我遇到的一个问题是尝试设置计
根据下面的数据,ClockKit 会生成一次 future 的 CLKComplicationTimelineEntry 项,但对于过去的时间点,会进行 24 次调用!这是为什么? 更多详情: 我注意
我有一个 MySQL 表,这个表有一个名为 datetime_utc 的 DATETIME 列。如您所料,它是 UTC 日期和时间。在我的 Bookshelf 模型中,我定义了一个虚拟 getter,
大家好,我是二哥呀! 昨天,一位球友问我能不能给他解释一下 @SpringBootApplication 注解是什么意思,还有 Spring Boot 的运行原理,于是我就带着他扒拉了一下这个注解的源
我是一名优秀的程序员,十分优秀!