I am doing a small exercise in which I register users through a form where I encrypt the password using the password_hash()
. There is everything good because I look at the database and I see that you have saved the encrypted password. The problem is that I don't know how to log in by verifying the hash since the way I have it, it generates another password and doesn't start the session.
Registration form code:
<html>
<head>
<title>Registro de usuarios</title>
<meta charset="utf-8">
</head>
<body>
<h2>Registro de usuarios</h2>
<form method="post" action="registroUsuarios.php">
<label for="nombre">Nombre de usuario:</label>
<input type="text" name="nombre"><br><br>
<label for="correo">Correo:</label>
<input type="email" name="correo"><br><br>
<label for="pass">Contraseña:</label>
<input type="password" name="pass"><br><br>
<input type="submit" value="Enviar">
</form>
</body>
</html>
Code where I register users:
<?php
require_once("BaseDatos.php");
$objetoBBDD = new BaseDatos();
if(empty($_POST["nombre"]))
$nombre = "NULL";
else
$nombre = "'".$_POST["nombre"]."'";
if(empty($_POST["correo"]))
$correo = "NULL";
else
$correo = "'".$_POST["correo"]."'";
if(empty($_POST["pass"]))
$pass = "NULL";
else
$pass = "'".$_POST["pass"]."'";
//esto es lo que he utilizado para encriptar
$passCifrada = password_hash($pass,PASSWORD_DEFAULT);
$passCifrada = "'".$passCifrada."'";
$consulta = 'INSERT INTO usuarios(idUsuario,nombreUsuario,correo,password) VALUES(null,'.$nombre.','.$correo.','.$passCifrada.');';
$objetoBBDD->consultarBD($consulta);
if($objetoBBDD->filasAfectadas()>0)
{
echo '<h3><font color="green">Usuario registrado correctamente</font></h3>';
}
else
{
echo '<h3>'.$objetoBBDD->comprobarErrores().'</h3>';
}
echo '<a href="formularioRegistro.html"><button>Volver</button></a>';
$objetoBBDD->cerrarConexion();
?>
Code to check the data and start the session:
<?php
session_start();
require_once("BaseDatos.php");
$objetoBBDD = new BaseDatos();
if(empty($_POST["nombre"]))
$nombre = "NULL";
else
$nombre = "'".$_POST["nombre"]."'";
if(empty($_POST["pass"]))
$pass = "NULL";
else
$pass = "'".$_POST["pass"]."'";
//esto es lo que no se hacer para comprobar el hash que ya tengo
$passCifrada = password_hash($pass,PASSWORD_DEFAULT);
$passCifrada = "'".$passCifrada."'";
$consulta = 'SELECT * FROM usuarios WHERE nombreUsuario='.$nombre.' AND password='.$passCifrada;
$objetoBBDD->consultarBD($consulta);
$fila = $objetoBBDD->extraerFilas();
if(strcmp($nombre,$fila["nombreUsuario"])==0 && strcmp($passCifrada,$fila["password"]==0))
{
$_SESSION["id"] = $fila["idUsuario"];
echo '<h2><font color="green">Sesion iniciada</font><br><br>';
echo 'Bienvenido '.$fila["nombreUsuario"].'</h2>';
}
else
{
echo '<h2><font color="red">Error al iniciar sesión</font></h2>';
echo '<h3><font color="red">Nombre de usuario o contraseña incorrecto</font></h3>';
}
echo '<a href="loginUsuarios.html"><button>Volver</button></a>';
$objetoBBDD->cerrarConexion();
?>
strong passwords
It
hash
is generated like this:To compare the passwords that you have sent by
$_POST
and the hash that was saved in your database is this way:Reference:
http://php.net/manual/en/function.password-verify.php
You should create a method that compares said hash, maybe you should first verify that the user exists. and if it exists, compare said hashes. This is an example:
I hope it helps you