-2
<?php
        // John123 is username, UDXddM is the password linked to that username. Same markup for all others.
        $login['John123'] = "UDX6672ddM";
        $login['Ramsey123'] = "iGO4u4234TC";
        $login['Nikita123'] = "KLWRES31BYKRBPJM";
        $login['Emma123'] = "PNYUhdhCHU34AQPSUYJ";
        $login['Anna123'] = "PNYZB512SGJkfDALT";
        $login['Peter123'] = "SNZasdVPB56HZSUhkET";
        $login['Bob123'] = "VLPBPH778UATAaGM";
        $login['Ron123'] = "FDSasArhCU90TPDSC";
        $login['Will567'] = "GQgCUF083GTZgsTFPG";
        $login['Oliver123'] = "VAGSE4234GKJXY";

        foreach()
        {

        }
        ?>

Using a foreach loop I want to see if the username matches the password (for example if I were to type Username: John123 and Password: UDX6672ddM in the login page I made I want to echo something. How do I check if the Username and Password match?

rutgerb19
  • 9
  • 3

1 Answers1

2
$user = "John123";
$password = "UDX6672ddM";

foreach ($login as $key => $value) {
    if ($key == $user) {
        if ($loginArray[$user] == $password) {
            // user and password match
        }
    }
}

This is an iteration over keys in the array instead of values, which is what the arrow syntax is used for. But why do you need to use a foreach? Why not simply check for each user you have if their details are in the array without a loop? EG:

$user = "John123";
$password = "UDX6672ddM";

if (array_key_exists($user, $login)) {
    if ($login[$user] == $password) {
        // user and password match
    } else {
        // user exists but password is wrong
    }
} else {
    // user doesn't exist
}
James Paterson
  • 2,652
  • 3
  • 27
  • 40