【问题标题】:Trouble in verifying hash password with the password user entered使用输入的密码验证哈希密码时遇到问题
【发布时间】:2017-08-05 12:28:10
【问题描述】:

我正在尝试使用用户输入登录的密码来验证存储在数据库中的哈希密码。但我没有成功。我正在使用 password_verify 来比较密码,但即使我输入正确的密码,它也没有给出正确的答案。 请帮帮我!!

       <?php
       print_r($_POST);
       include('connect.php');
       var_dump($_POST);
       print_r($_POST);
       $tbl_name = 'userC';


      if(isset($_POST["USERNAME"]) && isset($_POST["USER_PASSWORD"]))
       {
          $username1 = $_POST["USERNAME"]; 
          $password1 = $_POST["USER_PASSWORD"];
       }

     // To protect MySQL injection
       $username1 = stripslashes($username1);
       $password1 = stripslashes($password1);



       $stid = oci_parse($conn, "SELECT * FROM $tbl_name where 
       user_name='$username1'");
       $result = oci_execute($stid);
      //$re = oci_fetch_all($stid,$abc);

       while(($row = oci_fetch_array($stid,OCI_BOTH)) != false )
        {
         $password = $row[6];
         $username = $row[2];
          $re = 1;
         }
        if(isset($password))
      {
         if (password_verify($password1, $password))
        {
           $re1=1;
         }
       else
          {
           $re1 =  0;
           }  
           }
        else
           {
             $re1 = 0;
             }

   // If result matched $username and $password, table row must be 1 row
        if($re >= 1 && $re1 >= 1)
        {
   // Register $username, $password and redirect to file "login_success.php"
      session_start();
        $_SESSION["username"] = $username;
      header("location:form.php");
    }
       if($re < 1) {
             $failed = 1;
              header("location:login.php?msg=failed");
        }
         if($re1 < 1) {
            $failed = 1;
           header("location:verify.php?msg1=failed");
        }

          ?>

【问题讨论】:

  • 请将password_hash($password1,PASSWORD_DEFAULT)结果和echo $password结果添加到您的问题中

标签: php


【解决方案1】:

从您的代码中删除 $password1 = stripslashes($password1);。在将输入的密码传递给password_verify(或password_hash)之前,您不应以任何方式修改输入的密码。

顺便说一句,stripslashes 并不能保护您免受 SQL 注入。改用准备好的语句和oci_bind_by_name

$stid = oci_parse($conn, "SELECT * FROM $tbl_name where user_name=:uname");
oci_bind_by_name($stid, ":uname", $username1);
$result = oci_execute($stid);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-19
    • 1970-01-01
    • 2018-10-13
    相关资源
    最近更新 更多