How do I change a variable within the foreach?

-2

Please, how do I change the $ logged variable inside if in foreach?

<?php

session_start();

$login = $_REQUEST["login"];
$senha = $_REQUEST["senha"];
$logado = FALSE;

$usuario = array(
    "login" => "admin", "senha" => "admin",
    "login" => "admin1", "senha" => "admin1",
    "login" => "admin2", "senha" => "admin2",

);

foreach ($usuario as $user => $value) {
    if ($value === $login and $value === $senha) {
        $logado = TRUE;
        break;
    }
}

if ($logado) {
    $_SESSION['logado'] = true;
    header("location: logado.php");
} else {
    echo '<h1>erro!</h1>';
}
?>
    
asked by anonymous 24.06.2018 / 16:18

2 answers

0

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>';
}
?>
    
24.06.2018 / 19:08
-1

Vlw dvd!

<?php

session_start();

$login = $_REQUEST["login"];
$senha = $_REQUEST["senha"];
$log1 = FALSE;
$log2 = FALSE;
$c =0;

$usuario = array(
    array("login" =>"ladmin", "senha" => "madmin"),
    array("login" => "ladmin1", "senha" => "madmin1"),
    array("login" => "ladmin2", "senha" => "madmin2"),
);

foreach ($usuario as $user => $v) {
    foreach ($usuario[$user] as $user2 => $value){       
        if ($value === $login ) {
            $log1 = TRUE;
        }
        if ($value === $senha ) {
            $log2 = TRUE;
        } 
    }
    if ($log1 and $log2) {
        break;
    }

}

if ($log1 and $log2) {
    $_SESSION['logado'] = true;
    header("location: logado.php");
} else {
    echo '<h1>erro!</h1>';
}

?>
    
25.06.2018 / 02:54