【问题标题】:basic error with password encryption in phpphp中密码加密的基本错误
【发布时间】:2023-03-23 09:09:02
【问题描述】:

我想使用 php 重置用户密码。我从 html 表单中获得了用户的当前密码和新密码。这是重置密码的php脚本。但即使用户输入正确的密码,它也总是执行else 部分。怎么样?任何解决方案?我知道可能有一个简单的错误,但我是新手,找不到任何错误。

 $uid = $_SESSION['uid'];
    $current_pass = $_POST['org_pass'];
    $new_pass = $_POST['new_pass'];

    if(isset($_POST['submit']))
    {
            $act_pass = $db_con->prepare("SELECT password FROM user WHERE u_id= ?");
            $act_pass->bindParam(1,$uid);

            $act_pass->execute();

            $actual_pass = $act_pass->fetchColumn();

            define('SALT', 'flyingrabbit');

            $typed_pass = md5(SALT.$actual_pass);

            if ($typed_pass == $current_pass)
            {
                $new_pass1 = md5(SALT . $new_pass);

                $res = $db_con->prepare("UPDATE user SET password= ? WHERE u_id=?");
                $res->bindParam(1,$new_pass1);
                $res->bindParam(2,$uid);

                $res->execute();

                 header("Location: profile.php"); 
                 exit;
            }
            else
            {


                   echo "<script type=\"text/javascript\">window.alert(\"You entered wrong password.\");window.location.href = 'profile.php';</script>";

             }

    }

【问题讨论】:

  • 您的意思是将$current_pass 替换为$actual_pass 吗?
  • 我想用$new_pass 替换$actual_pass$current_pass ..user 以 html 形式输入,这是他的密码。 $actual_pass 是存储在数据库中的用户密码。
  • 您指定: if ($typed_pa​​ss == $current_pass) 您是否 100% 确定这是您想要的?在我看来,您应该这样做: if ($typed_pa​​ss == $actual_pass) 对于 typed_pa​​ss,您将数据库中的密码转换为哈希。您应该对输入执行此操作,而不是数据库中的内容。
  • 这里有很多问题。首先,您只是假设您正在获得一排回来。你的盐是一个常数,这是不安全的,你使用的 md5 也是不安全的。最后成功和失败的登录有效地做同样的事情:带你到 profile.php 而不启动会话或设置任何类型的 var。
  • @jeroen 好点;在这种情况下,我会将 OP 转至 stackoverflow.com/questions/19103340/…

标签: php mysql passwords


【解决方案1】:

这看起来不对:

$actual_pass = $act_pass->fetchColumn();

// ...

$typed_pass = md5(SALT.$actual_pass);

if ($typed_pass == $current_pass)

您正在对从数据库中获得的信息进行哈希处理,我假设这些信息已经被哈希处理。

你可能想要:

$actual_pass = $act_pass->fetchColumn();

// ...

$typed_pass = md5(SALT.$current_pass);

if ($typed_pass == $actual_pass)

注意md5 is not recommended 用来散列密码。

【讨论】:

  • 哦..对。谢谢。关于md5。我搜索了谷歌并阅读了 1 或 2 本关于加密的书籍,他们推荐 md5。怎么样?
  • @HungryDB 检查我答案底部的链接。
【解决方案2】:

您应该比较散列的 $current_pass 和 **$actual_pas**s。

替换

$typed_pa​​ss = md5(SALT.$actual_pass);$typed_pa​​ss = md5(SALT.$current_pass); $typed_pa​​ss == $current_pass$typed_pa​​ss == $actual_pass

【讨论】:

    【解决方案3】:

    它转到 else 语句,因为您比较 $typed_pass == $current_pass 但在上一行您这样做 $typed_pass = md5(SALT.$actual_pass) 您将哈希、加盐密码与纯文本密码进行比较

    【讨论】:

      猜你喜欢
      • 2015-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多