如何添加将某个电子邮件和密码重定向到PHP中的其他页面的条件?

问题描述:

I want to add a condition where if the email is admin@example.com and password is admin, then the admin will be redirected to admin.html, which is different to what a normal user will be redirected to (user.html). P.S. the admin and users are in the same table. Thanks in advance.

<?php
    require_once ('../../connect.php');
    $user_email = $_POST['user_email'];
    $user_password = $_POST['user_password'];

    if ($user_email != NULL AND $user_password != NULL) 
    {
        $login = "SELECT * FROM tblusers where user_email = '$user_email' AND user_password = '$user_password' AND user_type=0";
        $result = mysqli_query($dbc, $login);

        if (mysqli_num_rows($result) >0 ) 
        {
            setcookie('user_email', $user_email);
            setcookie('user_password', $user_password);

            echo '<script type="text/javascript"> window.location ="register.php"; </script>';
        }

        else
        {
        echo '<script type="text/javascript"> alert("The email or password you have entered may be incorrect"); window.location ="login.html"; </script>';  
        }
    }

    else ($user_email != NULL AND $user_password != NULL) 
    {
        $login = "SELECT * FROM tblusers where user_email = '$user_email' AND user_password = '$user_password' AND user_type=1";
        $result = mysqli_query($dbc, $login);

        if (mysqli_num_rows($result) >0 ) 
        {
            setcookie('user_email', $user_email);
            setcookie('user_password', $user_password);

            echo '<script type="text/javascript"> window.location ="members.php"; </script>';
        }

        else
        {
        echo '<script type="text/javascript"> alert("The email or password you have entered may be incorrect"); window.location ="login.html"; </script>';  
        }
    }

    else
    {
        echo '<script type="text/javascript"> alert("Please enter your email and password in the relative fields"); window.location ="login.html"; </script>';      
    }

    mysqli_close($dbc);
?>

Hmm, your post makes it really difficult to properly provide an answer, but I will try. Before that, know that @RiggsFolly really has made the most important point - get a better tutorial. I would use comments because there are some things that could be clarified but my reputation does not allow me to do that yet. So here goes an attempt at an answer.

  • What exactly is the logic you are trying to implement? It seems to roughly be:

    if (user provides credentials AND credentials exist in database AND credentials are for user_type == 0) {
        save credentials;
        send user to registration page;
    } else if (user provides credentials AND credentials exist in database AND credentials are for user_type == 1) {
        save credentials;
        send user to members page;
    } else {
        send user to login page;
    }
    
We can streamline this logic a bit:
if (user has provided credentials) { // if this fails, user is sent to login page
    // Now check if credentials exist in database
    // Notice I am using comments? Use them to make your code more readable and to better explain what you're doing/what you did!!!
    // Query the database only for matching username and password first.
    $login = "SELECT * FROM tblusers where user_email = '$user_email' AND user_password = '$user_password'";
    $result = mysqli_query($dbc, $login);
    // If this returns a match, then check for user_type. Otherwise, prompt user to provide correct credentials.
    if (mysqli_num_rows($result) > 0 ) {
        // Obtain the results of the query in an associative array so that you can easily access the value of 'user_type'
        $row = mysqli_fetch_assoc($result);
        // We have confirmed that the credentials exist. So we can save them
        // But as RiggsFolly correctly points out, PLEASE look for alternatives more secure than cookies
        save credentials;
        // Now check the value of user_type and respond accordingly
        if ($row["user_type"] == 1) { // admin
            send user to admin page; // which seems to be members.php;
        } else { // user
            // I assume there is no other user_type.
            // If there is, make this an elseif statement that checks if user_type == 0
            send user to user page; //which seems to be register.php
        }
    } else {
        display message that credentials are incorrect;
        send user to login page;
    }
} else {
    send user to login page;
}

Again, read ALL the links provided by @RiggsFolly and implement them. As he pointed out, we try to improve your code not to write it for you, which is why I tried to stick to the code you provided.

I do hope this helps you. Wish you the best as you learn.