In the first instance, I hope you noticed that the following emails have a space at the end:'gon@gon.com ''gilete@gilete.com 'So if the user places the same mail, but without the space at the end, it will not pass the validation. Out of that, I've tried each of the emails/contraseñas and it's gone smoothly.<?php
define('USUARIOS',array(
array(
'email' => 'admin@admin.com',
'password' => convertir_hash('iloveu')
),
array(
'email' => 'donald@donald.com',
'password' => convertir_hash('m4k3Am3r1caGr3atAg41n')
),
array(
'email' => 'gilete@gilete.com',
'password' => convertir_hash('ErF4ryS1empr3')
),
array(
'email' => 'gon@gon.com',
'password' => convertir_hash('Fatality')
),
)
);
function convertir_hash($pwd){
return password_hash($pwd, PASSWORD_DEFAULT);
}
$email_Decod = 'gon@gon.com';
$contra_Decod = 'Fatality';
foreach(USUARIOS as $vector){
if($vector['email']===$email_Decod && password_verify($contra_Decod,$vector['password'])){
echo 'OK';
// header("location: principal.php");
exit();
}
}
echo "<script language='javascript'> alert('El CORREO $email_Decod Y SON INCORRECTOS'); </script>";
$errors = "ERROR:VUELVE A INTRODUCIR CORREO Y CONTRASEÑA!";
EDIT: If you do not want a foreach for login, you can make use of https://www.php.net/manual/es/function.array-search.php And putting it in a function would be like this:function login($email, $password) {
$key = array_search($email, array_column(USUARIOS, 'email'));
if (is_null($key)) return false;
return password_verify($password, USUARIOS[$key]['password']);
}
And the evidence:$tests = array(
array(
'email' => 'noexiste@admin.com',
'password' => 'incorrecto'
),
array(
'email' => 'admin@admin.com',
'password' => 'iloveu'
),
array(
'email' => 'donald@donald.com',
'password' => 'm4k3Am3r1caGr3atAg41n'
),
array(
'email' => 'gilete@gilete.com',
'password' => 'ErF4ryS1empr3'
),
array(
'email' => 'gon@gon.com',
'password' => 'Fataasdlity'
),
array(
'email' => 'gon@gon.com',
'password' => 'Fatality'
),
);
foreach($tests as $test) {
$login = login($test['email'], $test['password']) ? 'TRUE' : 'FALSE';
echo "Email: {$test['email']}\n";
echo "Login Status: {$login}\n\n";
}
Salida:
Email: noexiste@admin.com
Login Status: FALSE
Email: admin@admin.com
Login Status: TRUE
Email: donald@donald.com
Login Status: TRUE
Email: gilete@gilete.com
Login Status: TRUE
Email: gon@gon.com
Login Status: FALSE
Email: gon@gon.com
Login Status: TRUE
And applied to your code:if (login($email_Decod, $contra_Decod)) {
header("location: principal.php");
exit();
}
echo "<script language='javascript'> alert('El CORREO $email_Decod Y SON INCORRECTOS'); </script>";
$errors = "ERROR:VUELVE A INTRODUCIR CORREO Y CONTRASEÑA!";