Hello people I have that if code:
$DifineUser = "admin";
$DifinePass = "admin";
$DifineUserSis = "sistemas";
$DifinePassSis = "sistemas";
if ($user === $DifineUser && $pass === $DifinePass) {
session_start();
$_SESSION["user"] = $user;
header("Location: home.php");
} else{
header("Location: entraragain.php");
$error = "El usuario o la contraseña son incorrectos";
}
And then down the other if
if ($user === $DifineUserSis && $pass === $DifinePassSis) {
session_start();
$_SESSION["user"] = $user;
header("Location: /rrhh/sistemas/home.php");
} else{
header("Location: entraragain.php");
$error = "El usuario o la contraseña son incorrectos";
}
It turns out that I left only one if, it takes me to the page, but if I leave the two if, it takes only the last if.
I don't know where the problem would be, in theory I would have to respect the if, if they are different variables or not?
Clarifying that the pass and user admin, admin is only for testing.
The problem is because calling the function
header
does not stop the execution process, but simply sets a response header.When your code runs, the process enters some of the "first block" conditions , but! does not stop, processing continues, the "second block" is processed which causes the previously sent header to be overwritten.
Solution:
Whenever you perform a redirect using
header
you must then terminate the execution process using, for example,exit
Example:
Hello, could you do something cleaner by defining another variable where you indicate the type of user and depending on the type of user, you redirect it something like this
In this I put a single if where I validate that the password and password and in a ternary if I validate the type of user depending on the type of user I redirect to where it has to if. Only if you need to define what type of user will use what.
The problem is that you are assigning a conditional to the variable
$user
before proceeding to the next block of code, you can validate it as follows in the same block usingelseif
the following way