Associative array with username and password - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Saturday, March 12, 2022

Associative array with username and password

I have the usernames and password stored in an associative array, I want to have a login system where it detects wrong password or the username does not exist. I have an if else code but is there anyway to make the code more shorter? I have this code on LoginValidator.php

    <?php
    $users = array(
        array("User" => "Test", "Password" => "123"),
        array("User" => "TestUser", "Password" => "x021"),
        array("User" => "Admin", "Password" => "admin"),
        array("User" => "user", "Password" => "user")
    );

    if ($_GET['username'] == $users[0]["User"] and $_GET['password'] == $users[0]["Password"]) {
        echo "Login Succesful";
    } else if ($_GET['username'] == $users[1]["User"] and $_GET['password'] == $users[1]["Password"]) {
       echo "Login Succesful";
    } else if ($_GET['username'] == $users[2]["User"] and $_GET['password'] == $users[2]["Password"]) {
   echo "Login Succesful";
    } else if ($_GET['username'] == $users[3]["User"] and $_GET['password'] == $users[3]["Password"]) {
        echo"Login Succesful";
    }
    ?>

This is the login.html

            <div class="login">
            <input type="text" placeholder="User Name" name="username"/>
            <input type="password" placeholder="Password" name="password"/>
            <input type="submit" value="Login"/>


source https://stackoverflow.com/questions/71442924/associative-array-with-username-and-password

No comments:

Post a Comment