我试图弄清楚如何在注册和登录系统上使用password_hash。
目前我正在使用这样的password_hash来注册我的用户。
$pass = $_POST['Pass'];
$hashed_password = password_hash($pass, PASSWORD_DEFAULT);
$stmt = $conn->prepare("INSERT INTO `usuario`(`Nick`, `Nombre_u`, `Apellidos`, `e-mail`, `Password`, `Domicilio`, `Colonia`, `Codigo_Postal`, `Cuidad`, `Estado`, `Telefono`) VALUES (?, ?, ?, ?, ?, ? , ?, ?, ?, ?, ?)");
$stmt->bind_param( "sssssssisss", $nick, $nombre, $apellidos, $mail, $hashed_password, $domicilio, $colonia, $cp, $cuidad, $estado, $telefono);
$stmt->execute();
header("Location: ../Registrado.php?Done=Welcome");
我以这种方式记录我的用户。
$usuario = $_POST["Nick"];
$contra = $_POST["Pass"];
$hashed_password = password_hash($contra, PASSWORD_DEFAULT);
$stmt = $conn->prepare("SELECT Nick, Password FROM usuario WHERE Nick = ? AND Password= ?");
$stmt->bind_param( "ss", $usuario, $hashed_password);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($a, $b);
if($stmt->fetch() == 0){
header("Location: ../Entrar.php?message=Error");
exit();
}
else {
session_start();
$_SESSION['Usuario'] = $a;
$_SESSION['estado'] = 'Autenticado';
header("Location: ../../Index.php");
exit();
}
我理解的方式是我的查询会做这样的事情。
首先将接受我的输入,例如:“123
”,然后 hashed_password 会将我的输入变成 Eg:”$2y$10$BvFW3ott5f7JvZ4rCa
“,我的查询将像这样完成他的工作。
SELECT Nick, Password FROM usuario WHERE Nick = 'User' AND Password= '$2y$10$BvFW3ott5f7JvZ4rCa'
但我仍然返回到我的登录表单,而不是登录我的用户。
我究竟做错了什么?