password_verify hash no coincide con la contraseña

He generado un hash de contraseña usando el siguiente código:

$hash = password_hash("test", PASSWORD_BCRYPT);

Luego lo almaceno en la base de datos usando 255 caracteres.

Luego trato de hacer el comparador para probar el inicio de sesión y falla. Solo me permite iniciar sesión usando un hash que acabo de generar algunas líneas antes, ninguna almacenada en la base de datos.

<?php 

//Database connection
require 'database.php';

//Handle logins
if ($_POST['login'])
{
    //Receive the login attempt
    $login_email = $_POST['login_email'];
    $login_password = $_POST['login_password'];

    //Get the password hash
    if ($statement = $mysqli->prepare("SELECT password FROM accounts WHERE email = ? LIMIT 1"))
    {
        $statement->bind_param("s", $login_email);
        $statement->execute();
        $statement->store_result();

        //Does the account exist?
        if ($statement->num_rows > 0)
        {
            $statement->bind_result($hash);
            $statement->fetch();

            //echo $login_password;
            echo $hash."<br>";
            //$hash = password_hash("test", PASSWORD_BCRYPT);
            //echo $hash."<br>";

            //Check the password hash
            if (password_verify($login_password, $hash))
            {
                echo '<br>Password is valid!';

                //Begin session
                session_start();
                $_SESSION["favcolor"] = "yellow";
            } 
            else 
            {
                echo '<br>Invalid password.';
            }
        }
        else
        {
            //Account doesn't exist warning
        }

        $statement->free_result();
        $statement->close();
    }
}

//Handle new registrations
if ($_POST['register'])
{
    //Receive the register attempt
    $register_email = $_POST['register_email'];
    $register_password_one = $_POST['register_password_one'];
    $register_password_two = $_POST['register_password_two'];

    //Check if email is already taken
    if ($statement = $mysqli->prepare("SELECT email FROM accounts WHERE email = ? LIMIT 1"))
    {
        $statement->bind_param("s", $register_email);
        $statement->execute();
        $statement->store_result();

        //Does the account exist?
        if ($statement->num_rows > 0)
        {
            //Account already exists warning
        }
        else
        {
            //Create the account
            if ($statement = $mysqli->prepare("INSERT INTO accounts (email, password) VALUES (?,?)"))
            {
                //Create bycrypt hash of password
                $hash = password_hash($register_password_one, PASSWORD_BCRYPT);

                //Insert new account
                $statement->bind_param("ss", $register_email, $hash);
                $statement->execute();
                $account_id = $statement->insert_id;
                $statement->close();

                //Begin session
                session_start();
                $_SESSION["favcolor"] = "yellow";
            }
        }
        $statement->free_result();
        $statement->close();
    }
}

//Handle logout
if ($_POST['logout'])
{
    session_unset();
    session_destroy();
}

?>

hash de la contraseña en la base de datos: $ 2y $ 10 $ xDnZIjzw8h.9utp3qyRlxezPd8jmK9k6Z5JuoVtooOpkPCBd.n6W6 hash de la contraseña que acaba se genera (obras): $ 2y $ 10 $ tolDQdeTQrTio8IJ0Wi9AuHN5Km28pSB5kUh5qfkdkOsDXP295H1K

No soy un experto en hashing. Solo trato de seguir las últimas recomendaciones. ¿Alguien podría decirme por qué el hash es diferente al de la base de datos?

Respuestas a la pregunta(1)

Su respuesta a la pregunta