【问题标题】:Change encrypted password?更改加密密码?
【发布时间】:2019-05-09 18:37:12
【问题描述】:

试图让用户可以更改密码,但由于密码已加密,用户必须先输入加密密码才能更改密码。

当用户点击“更改密码”时,他们会被发送到 change.php,然后再返回到 profile.php?error=wrong_pw

不习惯散列密码,因为到目前为止我只用 php 做过“简单”的网站,没有任何安全性。这也是为什么这很容易被黑客利用。

Change.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');
}

profile.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"; ?>

【问题讨论】:

  • 您的问题或问题究竟是什么?
  • 对;什么没有按照您想要的方式工作?你得到什么结果?检查错误?你的问题不清楚(对我来说)。
  • 您的要求尚不清楚。如果您还没有stackoverflow.com/help 以及其中的相关链接,那么对您有好处。请通读一遍,您将了解 Stack Overflow 是如何工作的,“学习技巧”。它会给你一个关于如何提出一个好问题的好主意,看看什么可以问,什么不应该问,以及对你的期望。这是为了帮助您在这里获得更好和积极的体验而制定和实施的,这是每个人都想要和追求的。

标签: php mysql sql session passwords


【解决方案1】:

嗯,我觉得你错过了加密的概念。

应该没有办法从哈希中检索字符串,因为它是一种单向算法。

重置密码的过程如下:

  1. 要求用户输入旧密码和新密码。
  2. 用户填写数据并提交。
  3. 用户输入的旧密码使用与您从数据库中对真实旧密码进行哈希处理的算法相同的算法进行哈希处理。
  4. 您比较数据库中真正的旧密码和用户输入的“旧”密码之间的哈希值。
  5. 如果它们匹配,则更新旧密码所在的注册表,并将其替换为新密码。

如果你检查一下,你会发现它是合乎逻辑的。祝你好运!

【讨论】:

    【解决方案2】:

    要更改/重置密码,用户必须先登录,然后他可以更新他/她的密码,提供当前密码。如果用户不知道他/她的密码,则必须完成忘记密码。通过这种方式,您可以验证密码仅由授权/有效人员更改。

    但是,在您的情况下,您可以更改此查询以更新密码而无需验证

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

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

    【讨论】:

      猜你喜欢
      • 2019-04-07
      • 2021-06-27
      • 1970-01-01
      • 1970-01-01
      • 2017-12-27
      • 1970-01-01
      • 2018-06-20
      • 2011-02-22
      • 1970-01-01
      相关资源
      最近更新 更多