Php 从md5更改为密码\u散列
我在学习创建cms的教程,但是讲师使用md5作为密码,我试图将其改为使用Php 从md5更改为密码\u散列,php,html,sql,pdo,Php,Html,Sql,Pdo,我在学习创建cms的教程,但是讲师使用md5作为密码,我试图将其改为使用password\u hash。我的sql数据库中使用的密码确实使用密码\u散列,但我对如何在登录页面中验证密码感到困惑 我已尝试在登录页面中将md5更改为密码\u哈希,但这不起作用,我还尝试了密码\u验证。我知道这些应该被使用,但不知道我应该在哪里改变什么 <?php session_start(); include_once('../includes/connection.php'); if (isset(
password\u hash
。我的sql数据库中使用的密码确实使用密码\u散列,但我对如何在登录页面中验证密码感到困惑
我已尝试在登录页面中将md5更改为密码\u哈希
,但这不起作用,我还尝试了密码\u验证
。我知道这些应该被使用,但不知道我应该在哪里改变什么
<?php
session_start();
include_once('../includes/connection.php');
if (isset($_SESSION['logged_in'])) {
?>
<html>
<head>
<title>CMS Tutorial</title>
<link rel="stylesheet" href="../assets/style.css"/>
</head>
<body>
<div class="container">
<a href="index.php" id="logo">CMS</a>
<br />
<ol>
<li><a href="add.php">Add Article</a></li>
<li><a href="delete.php">Delete Article</a></li>
<li><a href="logout.php">Logout</a></li>
</ol>
</div>
</body>
</html>
<?php
}else{
if (isset($_POST['username'], $_POST['password'])) {
$username = $_POST['username'];
//$password = md5($_POST['password']);
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);
if (empty($username) or empty($password)){
$error = 'All fields are required!';
}else{
$query = $pdo->prepare("SELECT * FROM users WHERE user_name = ? AND user_password = ?");
$query->bindValue(1, $username);
$query->bindValue(2, $password);
$query->execute();
$num = $query->rowCount();
if ($num == 1) {
$_SESSION['logged_in'] = true;
header('Location: index.php');
exit();
//user entered correct details
}else{
//user entered false details
$error = 'Incorrect details!';
}
}
}
?>
<html>
<head>
<title>CMS Tutorial</title>
<link rel="stylesheet" href="../assets/style.css"/>
</head>
<body>
<div class="container">
<a href="index.php" id="logo">CMS</a>
<br /><br />
<?php if (isset($error)) { ?>
<small style="color:#aa0000"><?php echo $error; ?></small>
<br /><br />
<?php } ?>
<form action="index.php" method="post" autocomplete="off">
<input type="text" name="username" placeholder="Username" />
<input type="password" name="password" placeholder="Password" />
<input type="submit" value="Login" />
</form>
</div>
</body>
<html>
<?php
}
?>
CMS教程
CMS教程
目前,我刚刚得到一份工作
“不正确的详细信息”错误
我已经创建,如果我使用密码,请验证我是否获得
“所有字段都是必需的错误”
要检查密码,必须使用
password\u verify()
函数,而不是password\u hash()
不久前,我写了一个典型的例子
正如您所见,代码非常简单:您只需选择密码,然后使用password\u verify()
请看您提供的代码多于与问题核心相关的代码。试着把它和你的要求联系起来。嗨,我试了你说的话,它出现了“未识别索引:密码”和它的行:如果($user&&password\u verify($\u POST['password'],$user['password']),我会错过什么?它的意思就是它所说的。必须是你数据库中的用户密码。是的,就是这样,只是有点愚蠢,非常感谢你的帮助,现在效果很好
$stmt = $pdo->prepare("SELECT * FROM users WHERE user_name = ?");
$stmt->execute([$_POST['username']]);
$user = $stmt->fetch();
if ($user && password_verify($_POST['password'], $user['password']))
{
$_SESSION['logged_in'] = true;
header('Location: index.php');
exit();
}else{
$error = 'Incorrect details!';
}