登录页面中不同成员组的不同位置

时间:2016-03-14 20:37:48

标签: php mysqli login members

**此代码可以将成功登录的用户定向到student_profile.php页面,但是我需要这样来检查尝试登录的用户类型,并根据该用户将该成员发送到适当的位置。在DB中我有一个名为account的字段,它有不同的成员类型;学生,房东和管理员,他们都有自己的页面。 **

<?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>";

            }
    }   

        ?>

1 个答案:

答案 0 :(得分:0)

使用if else或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;   
        }