根据用户类型重定向到不同页面的登录


Login that redirects on different pages depending on the usertype

我正在尝试开发网站中的登录模块,它实际上不是任何典型的登录模块。在我的登录表单中,有一个下拉框,您可以选择您是哪种类型的用户。在那里,它显示"教职员工"、"系主任"answers"主管"。每个用户都有不同的主页。这是我的login.php代码:

<?php
session_start(); // Starting Session
$error=''; // Variable To Store Error Message
if (isset($_POST['login'])) {
    if (empty($_POST['username']) || empty($_POST['password'])) {
            $error = "Username or Password is invalid";
    }else{
        // Define $username and $password
        //$user_type=$_POST['user_type'];
        $username=$_POST['username'];
        $password=$_POST['password'];
        // Establishing Connection with Server by passing server_name, user_id and password as a parameter
        $connection = mysql_connect("localhost", "root", "");
        // To protect MySQL injection for Security purpose
        //$user_type = stripcslashes($user_type);
        $username = stripslashes($username);
        $password = stripslashes($password);
        //$user_type = mysql_escape_string($user_type);
        $username = mysql_real_escape_string($username);
        $password = mysql_real_escape_string($password);
        // Selecting Database
        $db = mysql_select_db("cpecs", $connection);
        // SQL query to fetch information of registerd users and finds user match.
        $query = mysql_query("select * from tblaccounts where password='$password' AND username='$username'", $connection);
        $rows = mysql_num_rows($query);
        if ($rows == 1) {
            $_SESSION['login_user']=$username; // Initializing Session
                if ($_POST['user_type']= Faculty) {
                    header("location: userhome.php"); //Redirecting to Faculty Home Page
                }else if ($_POST['user_type']= Department) {
                    header("location: adminhome.php"); //Redirecting to Department Head Home Page
                }else if ($_POST['user_type']= Supervisor) {
                    header("location: supervisorhome.php"); // Redirecting to Supervisor Home Page
                }else{
                    $error = "Oops! It seems that you are not an authorized user.";
                }
        }
    mysql_close($connection); // Closing Connection
    }
}   
?>

我刚刚从数据库中的tbluser_type调用了usertype的值,将其显示在下拉框中。这是我的代码

<?php 
    include('connection.php');
        $sql = "SELECT * FROM tbluser_type";
        $result = mysqli_query($conn, $sql);
        $option_usertype="";
        while($row = mysqli_fetch_array($result)){         
            $option_usertype= $option_usertype."<option> $row[1] </option>";
        }
?>

请更改代码

 if ($rows == 1) {
            $_SESSION['login_user']=$username; // Initializing Session
                if ($_POST['user_type'] == 'Faculty') {
                    header("location: userhome.php"); //Redirecting to Faculty Home Page
                }else if ($_POST['user_type'] == 'Department') {
                    header("location: adminhome.php"); //Redirecting to Department Head Home Page
                }else if ($_POST['user_type'] == 'Supervisor') {
                    header("location: supervisorhome.php"); // Redirecting to Supervisor Home Page
                }else{
                    $error = "Oops! It seems that you are not an authorized user.";
                }
        }

您的错误

if ($_POST['user_type']=Faculty) {

if ($_POST['user_type'] == 'Faculty') {

获取用户类型

   $option_usertype=""; 
    while($row = mysqli_fetch_array($result)){ 
      $option_usertype .= "<option> ".$row['user_type']." </option>"; 
    } 

$row['user_type']到您的数据库字段名