phpmysqliauthenticationmembers

different locations for different members groups from sign in page


**This code enables a successfully logged in user to be directed to the student_profile.php page however i need this to check the type of user that is trying to log in and according to that send that member to the appropriate location. In the DB i have a field called account which has the different member types; student,Landlord and Administrator and they all have their own pages. **

<?php
     include ("connect.php");

    if (isset($_POST["user_login"]) && isset ($_POST["user_pass"])){
        // formatting field via reg replace to ensure email and password only conisists of letters and numbers preg_replace('#[^A-Za-z0-9]#i','', 
        $login_user = $_POST["user_login"];
        $login_password = $_POST["user_pass"];


        // password is encryted in DB (MD5) therefore user inputted password will not match encryted password in DB - we have to assign new var
        $decrypted_password = md5($login_password);

    // Query which finds user (if valid) from DB - Achieving authentication via username and password       

    $user_query = mysqli_query($connect, "SELECT * FROM users WHERE email = '$login_user' AND password = '$decrypted_password' AND closed = 'no' LIMIT 1"); 

        $check_user = mysqli_num_rows($user_query); // checking to see if there is infact a user which those credentials in the DB
            if ($check_user==1){

            while ($row = mysqli_fetch_array($user_query)){
                    $id = $row['user_id'];
                }
                // if the user credentials are correct, log the user in:
                $_SESSION["user_login"] = $login_user;
                    header( "Location:profile_student.php" ); // refresh page
                exit;   
            } 
            else {
                echo "<div class='wrong_login'>
                            <p> Email or password is incorrect, please try again. </p>
                         </div>";

            }
    }   

        ?>

Solution

  • Use if else or switch case :

    if ($check_user==1){
    
            while ($row = mysqli_fetch_array($user_query)){
                    $id = $row['user_id'];
                    $user_type = $row['account'];
                }
                // if the user credentials are correct, log the user in:
                $_SESSION["user_login"] = $login_user;
                // check the user type and redirect according to it
                if($user_type == "student"){
                 $redirection_page = "student.php";
               } elseif ($user_type == "Landlord"){
                  $redirection_page = "landlord.php";
               } elseif ($user_type == "Administrator"){
                   $redirection_page = "administrator.php";
               } else {
                   $redirection_page = "default.php";
               }
                    header( "Location:{$redirection_page }" ); // refresh page
                exit;   
            }