Home > Enterprise >  User and Admin login if else statement
User and Admin login if else statement

Time:06-06

I'm trying to detect and differentiate the login of the normals users and admin. The user only shows "welcome user" and when an admin log-in's it shows all the users. I've tried if else but the statement is wrong it dosent work.

LoginValidator.php

    <?php
    session_start();

    $users = array(
        array("User" => "admin", "Password" => "admin"),
        array("User" => "administrator", "Password" => "administrator"),


    );

    $mergedUsers = array_merge($users, $_SESSION['newUsers']);

    $userName = $_POST['username'];
    $msg = "";
    $codeinput = $_POST['code'];


    foreach ($mergedUsers as $value) {
        if ($userName == $value['User'] && $_POST['password'] == $value['Password']) {
            if ($codeinput == $_SESSION['captcha']) {
                echo "Captcha valid";
                echo "<p>Login successful<3</p>";
                if ($value['User'] == $users) {
                    echo "Welcome admin!";
                    echo print_r($mergedUsers);
                    echo print_r($users);
                    echo '<pre>' . print_r($_SESSION, TRUE) . '</pre>';
                } else
                    echo "Welcome user!";
            } else {
                echo "Captcha NOT valid";
            }
        } else if ($userName == $value["User"] and $_POST['password'] != $value["Password"]) {
            //$msg = "<p>Wrong Password</p>";
            echo "Wrong password";
        }
    }
    ?>

Login.php

 <form action="LoginValidator.php" method="POST">
    <div >
    <h3>Login Here</h3>

    <label for="username">Username</label>
    <input type="text" placeholder="User name" id="username" name="username" required>

    <label for="password">Password</label>
    <input type="password" placeholder="Password" id="password" name="password" required>

    <p>Enter this number: <?php echo $_SESSION['captcha']; ?></p>
    <p><input type="text" name="code" required />



        <button>Log In</button>

    <p> Don't have any account yet? <br> <a href="registration.php"> Register Here </a> </p>

    </div>
</div>
</form>

CodePudding user response:

You may try to type the value instead

  if ($value['User'] == "ADMIN") {
                    echo "Welcome admin!";
                    echo print_r($mergedUsers);
                    echo print_r($users);
                    echo '<pre>' . print_r($_SESSION, TRUE) . '</pre>';
                } else
                    echo "Welcome user!";

CodePudding user response:

I found problem in the if condition where you are checking for the admin.

if ($value['User'] == $users) {

Here, you are comparing a value with an array, which will not work. This problem could be solved by comparing an array with an array.

For that, you just have to change above if condition to:

if (in_array($value, $users)) {

The in_array() method will check whether $value array is present in $user array and will then return true if present, false if not.

I hope this will resolve your problem.

  • Related