dongwang6837 2016-02-06 10:05
浏览 47
已采纳

在我现有的登录页面上检查phpass是否正确

I'm trying to add Phpass to my website but no matter what I do I can't get the $check boolean to return true to let me actually log in, So far I've managed to encrypt my password with it and store it on the database but checking against it is failing.

<?php

if(isset($_POST['Login'])){

    $EM = $_POST['Email'];

    // Password from form input
    $PW = $_POST["Password"];

    // Passwords should never be longer than 72 characters to prevent DoS attacks
    if (strlen($PW) > 72) { die("Password must be 72 characters or less"); }

    // Just in case the hash isn't found
    $stored_hash = "*";

    // Retrieve the hash that you stored earlier
    $stored_hash = "this is the hash we stored earlier";

    // Check that the password is correct, returns a boolean
    $check = $hasher->CheckPassword($PW, $stored_hash);

    if ($check) {
    // passwords matched! show account dashboard or something

    $result = $con->query("select * from user where Email='$EM' AND Password='$PW'");

    $row = $result->fetch_array(MYSQLI_BOTH);

    session_start();

    $_SESSION["UserID"] = $row['UserID'];

    header('Location: Account.php');


    } else {

     // passwords didn't match, show an error
    header('Location: Fail.php');
    }


}

Because I've been trying to add to an existing login I wonder if I have excess code which is just breaking it? Or maybe I just messed it up all together as no matter what I try when logging in the only thing that will load is

header('Location: Fail.php');:|

Thanks in advance!

Edit: Right, I have a register file that saves the hashed password to the database:

if(isset($_POST['Register'])){

    session_start();
    $FName  = $_POST['First_Name'];
    $LName  = $_POST['Last_Name'];
    $Email  = $_POST['Email'];

    // In this case, the password is retrieved from a form input
    $PW = $_POST["Password"];

    // Passwords should never be longer than 72 characters to prevent DoS attacks
    if (strlen($PW) > 72) { die("Password must be 72 characters or less"); }

    // The $hash variable will contain the hash of the password
    $hash = $hasher->HashPassword($PW);

    if (strlen($hash) >= 20) {

    // Store the hash somewhere such as a database
    // The code for that is up to you as this tutorial only focuses on hashing passwords
    $sql = $con->query("INSERT INTO user (Fname, Lname, Email, Password)Values('{$FName}','{$LName}', '{$Email}', '{$hash}')");

    } else {

     // something went wrong

    }

    echo $hash;

//  $StorePassword = password_hash($PW, PASSWORD_BCRYPT, array('cost' => 10));



    header('Location: Login.php'); //Redirect here when registering

And then I wish to read it from the database, compare it to the password entered etc.

How do I pull that info from the mysqli db to compare it? and hopefully make it work?

This is the tutorial I was following: https://sunnysingh.io/blog/secure-passwords

  • 写回答

1条回答 默认 最新

  • dongpankao6133 2016-02-06 17:30
    关注

    if(isset($_POST['Login'])){
    
        $EM = $_POST['Email'];
    
        // Password from form input
        $PW = $_POST["Password"];
    
        // Passwords should never be longer than 72 characters to prevent DoS attacks
        if (strlen($PW) > 72) { die("Password must be 72 characters or less"); }
    
        $result = $con->query("select * from user where Email='$EM'");
    
        $row = $result->fetch_array(MYSQLI_BOTH);
    
        if ($row) {
    
            // Check that the password is correct, returns a boolean
            $check = $hasher->CheckPassword($PW, $row['Password']);
    
            if ($check) {
                // passwords matched! show account dashboard or something
    
                session_start();
    
                $_SESSION["UserID"] = $row['UserID'];
    
                header('Location: Account.php');
                exit;
            }
        }
    
         // passwords didn't match, show an error
        header('Location: Fail.php');
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化