#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2017
    Location
    Hilliard, Ohio
    Posts
    2
    Rep Power
    0

    password_verify($password, $hash) not working


    So I originally put this on StackOverflow and it got a pretty negative response, and I never really got an answer
    so I was hoping I could get on here.

    So I have a Registration and Login page

    Registration (Working)

    Code:
    <?php 
        include("assets/includes/conn.php");
    
        $user = $_POST['username'];
        $pass = $_POST['pass'];
        $cPass = $_POST['c-pass'];
        $email = $_POST['email'];
    
        $options = [
            'cost' => 11
        ];
    
        if($pass == $cPass){
            
            $stmt = $conn->prepare("INSERT INTO users (username, pass, email) VALUES (?, ?, ?)");
            $stmt->bind_param("sss", $user, $h_p, $email);
            
            $user = $_POST['username'];
            $h_p = password_hash($pass, PASSWORD_DEFAULT, $options)."\n";
            $email = $_POST['email'];
            $stmt->execute();
            
            echo "Created";
            echo $h_p;
            $stmt->close();
            $conn->close();
            var_dump($_POST);
        }
    ?>
    Login (not working)

    Code:
    <?php 
        include("assets/includes/conn.php");
    
        $username = $_POST['username'];
        $password = $_POST['pass'];
    
        $sql = "SELECT * FROM users WHERE username = '$username'";
        $result = $conn->query($sql);
    
        if ($result->num_rows == 1){
            $row = $result->fetch_assoc();
            $hash = $row['pass'];
            
            if(password_verify($password, $hash)){
                echo "Yes";
            } else {
                echo "No<br/>";
                echo "" . $hash . "<br/>";
                echo $password;
                var_dump($_POST);
            }
        }
    ?>
    I can't figure out what the issue is, I have this for more information on the database, any information on how to fix this would be greatly appreciated
  2. #2
  3. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,333
    Rep Power
    9645
    You got a negative response on SO because all you did was say "this isn't working" and post code. People on SO don't like discussions or Q&A - they want to write an answer and move on.
    How To Ask Questions

    I don't see the problem either. What do you get with
    PHP Code:
    echo "No<br/>";
    var_dump($_POST);
    var_dump(array("hash" => $hash"password" => $password"crypt" => crypt($password$hash))); 
    And what password are you using?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2016
    Posts
    92
    Rep Power
    5
    The problem is due to this copy/paste, I'm not looking at the code I am running, nonsense (sorry to sound like Jacq....) -

    $h_p = password_hash($pass, PASSWORD_DEFAULT, $options)."\n";
    You have append a new line to the end of the hash value.

    Programming requires that you WRITE the code that does what you want and you must know what each line of code actually does.

    Comments on this post

    • requinix agrees : :chomp:
  6. #4
  7. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,333
    Rep Power
    9645
    Originally Posted by DSmabismad
    The problem is due to this copy/paste,
    Ah. AH!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2017
    Location
    Hilliard, Ohio
    Posts
    2
    Rep Power
    0
    Originally Posted by DSmabismad
    The problem is due to this copy/paste, I'm not looking at the code I am running, nonsense (sorry to sound like Jacq....) -



    You have append a new line to the end of the hash value.

    Programming requires that you WRITE the code that does what you want and you must know what each line of code actually does.
    Thansk so much, it works!

IMN logo majestic logo threadwatch logo seochat tools logo