更改加密密码?

bttbmeg0  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(394)

试图让用户可以更改他们的密码,但由于密码是加密的,用户不能更改它没有输入加密的密码第一。
当用户点击“更改密码”时,他们会被发送到change.php,然后返回到profile.php?error=wrong\u pw
不习惯散列密码,因为到目前为止我只使用php做过“简单”的站点,没有任何安全性。这也是为什么这很容易被黑客利用的原因。
更改.php

<?php
session_start();

require 'dbc.php';
if (!isset($_POST['password']) || !isset($_POST['newpassword']) ||   !isset($_POST['renewpassword'])) {
header('Location: ../profile.php?error=wrong_info');
exit();
}

$user = $_SESSION['id'];
$password = $_POST['password'];
$newpassword = $_POST['newpassword'];
$renewpassword = $_POST['renewpassword'];
$sql = "SELECT * FROM users WHERE id='$user' AND pwd='$password'";
$result = mysqli_query($conn, $sql);
if($row = mysqli_fetch_assoc($result)){

if($newpassword == $renewpassword){
if($newpassword != ""){
  $newsql = "UPDATE users SET pwd='$newpassword' WHERE id='$user'";
  $newresult = mysqli_query($conn, $newsql);
  header('Location: ../index.php');

}
else{
  header('Location: ../profile.php?error=empty');
  exit();
}

} 
else{
header('Location: ../profile.php?error=match');
exit();
}

} else{

header('Location: ../profile.php?error=wrong_pw');
}

配置文件.php

<?php require "inc/header.inc.php"; ?>
<?php
if(isset($_GET['u'])){
$u = $_GET['u'];
}else{
$u = $_SESSION['id'];
}

$sql = "SELECT * FROM users WHERE id='$u'";
$result = mysqli_query($conn, $sql);
$user = mysqli_fetch_assoc($result);

?>

<div class="container mt-3">
<table class="table">
<tr>
  <th>Email</th>
  <th>Username</th>
  <?php
    if ($u === $_SESSION['id']) {
      echo "<th>Password</th>";
    }
  ?>
</tr>
<tr>
  <td><?php echo $user['email']; ?></td>
  <td><?php echo $user['uid']; ?></td>
  <?php
    if ($u === $_SESSION['id']) {
      echo '<th><a href="javascript:;" class="btn btn-danger" data-toggle="modal" data-target="#leModal">Change</a></th>';
    }
  ?>
</tr>
</table>
</div>
 <form action="change_theme.php" method="post">
 <center><button class="btn btn-primary themebutton" type="submit" value="Change theme" action="change_theme.php">Change theme</button></center>
 </form>
 <div class="modal fade" id="leModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
  <div class="modal-header">
    <h5 class="modal-title" id="exampleModalLabel">Change Password</h5>
    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
      <span aria-hidden="true">&times;</span>
    </button>
  </div>
  <div class="modal-body">
    <form class="article_q" action="engine/change.php" method="post">
      <input type="hidden" name="user" value="<?php echo $_SESSION['id']; ?>">
      <input type="password" name="password" placeholder="Current Password"><br><br>
      <input type="password" name="newpassword" placeholder="New Password"><br><br>
      <input type="password" name="renewpassword" placeholder="Re:Password"><br><br>
      <button class="btn btn-danger" value="submit">Save</button>
  </div>
  <div class="modal-footer">
    </form>
  </div>
</div>
</div>
</div>
<?php require "inc/footer.inc.php"; ?>
zkure5ic

zkure5ic1#

嗯,我觉得你缺少了加密的概念。
应该没有办法从散列中检索字符串,因为它是单向算法。
重置密码的过程如下:
要求用户键入旧密码和新密码。
用户填写数据并提交。
用户输入的旧密码使用与从数据库中散列真实旧密码相同的算法进行散列。
比较数据库中真实的旧密码和用户输入的“旧”密码之间的哈希值。
如果它们匹配,则更新旧密码所在的注册表,并用散列的新密码替换。
如果你检查它,你会发现它是合乎逻辑的。祝你好运!

vecaoik1

vecaoik12#

要更改/重置密码,用户必须先登录,然后才能更新其密码,并提供当前密码。如果用户不知道他/她的密码,则必须忘记密码。通过这种方式,您可以验证密码是否仅由授权/有效人员更改。
但是,在您的情况下,您可以将此查询更改为更新密码,而无需验证

$sql = "SELECT * FROM users WHERE id='$user'";

最好在更新密码之前通过电子邮件/短信验证用户。

相关问题