Search code examples
phphashpassword-hash

How to use $hash to encrypt password


How do I make this $hash the password the user enters when they sign up to the website? I've already got the log in working with $hash which works fine when I tested it with a manually inputted $hash password but can't get this form to create the encrypted passwords.

<?php
if(isset($_POST['emailaddress'],$_POST['password'],$_POST['name'])){
    $result= $pdo->prepare('INSERT INTO user (emailaddress, password, name )
                        VALUES(:emailaddress, :password, :name)');
    $hash = password_hash(password, PASSWORD_DEFAULT);
    unset($_POST['submit']);
    $result->execute($_POST);
    header("Location:admin.php");
}

Solution

  • Add $hash into the $_POST before you execute the query

    $_POST['password'] = $hash;
    $result->execute($_POST);
    

    Even better is what kingkero commented, that way you avoid unwanted values in the $_POST