What you are doing is a simple array and trying to get two distinct values where $value
can be "login" or "password", and since associative values are all equal, foreach
will return only the last one .
$usuario = array(
"login" => "admin", "senha" => "admin",
"login" => "admin1", "senha" => "admin1",
"login" => "admin2", "senha" => "admin2",
); ↑ ↑
mesmos nomes mesmos nomes
What you need to do is create an array where each index has a separate "login" => "valor", "senha" => "valor"
fault entry:
$usuario[] = array("login" => "ladmin", "senha" => "sadmin");
$usuario[] = array("login" => "ladmin1", "senha" => "sadmin1");
$usuario[] = array("login" => "ladmin2", "senha" => "sadmin2");
And in foreach
you check "login" and "password" using brackets:
foreach ($usuario as $user => $value) {
if ($value['login'] === $login and $value['senha'] === $senha) {
$logado = TRUE;
break;
}
}
Then the code looks like this:
<?php
session_start();
$login = $_REQUEST["login"];
$senha = $_REQUEST["senha"];
$logado = FALSE;
$usuario[] = array("login" => "ladmin", "senha" => "sadmin");
$usuario[] = array("login" => "ladmin1", "senha" => "sadmin1");
$usuario[] = array("login" => "ladmin2", "senha" => "sadmin2");
foreach ($usuario as $user => $value) {
if ($value['login'] === $login and $value['senha'] === $senha) {
$logado = TRUE;
break;
}
}
if ($logado) {
$_SESSION['logado'] = true;
header("location: logado.php");
} else {
echo '<h1>erro!</h1>';
}
?>