dongqiang2069 2014-04-08 17:19
浏览 24
已采纳

使用所有不同的重定向页面进行登录会话

) I'm working on a login system that has 3 users(Admin, Teacher and Student) all of these users will have their own functions and interface. I would like to have one login for all these users together if this is possible i'm not sure.

Im my database there is a row Role('Admin','Teacher','Student') i want the login direct based on the "role" so the admin will be redirect to the admin homepage, the teacher to the teacher homepage and the student to the student homepage. The admin pages can only be viewed by the admin the teacher pages by the teacher and the student pages only by the students. You probably get what I mean.

Question:: How can i make the login so that it will check if it is a admin/teacher/student? and when they are logged in how can i make the specific homepage for that user vieuwable only by that type of user?

<?php
session_start();
$mysqli=new MySQLi("localhost", "root", "", "hws");
$role="";

$username=filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING);
$password=filter_input(INPUT_POST, 'password', FILTER_SANITIZE_STRING);

if($query=$mysqli->prepare("SELECT `role` FROM members WHERE username=? AND password=?"))
{
    $query->bind_param("ss", $username, $password);
    $query->execute();
    $query->bind_result($role);
    $query->fetch();
}
else
{
    echo "Errors in the Query. ".$mysqli->error;
    die();
}

    if($role!="")
    {
        $_SESSION['ingelogt']=$username;
        $_SESSION['user_role']=$role;
        $location="$role.php"; // If role is admin this will be admin.php, if student this will be student.php and more.
        header("location: $location"); // Redirect to the respective pages.
    }
    else
    {
        echo "Invalid password, username combination";
    }

?>

and for the userpage a example

<?php
session_start()
if(!isset($_SESSION['ingelogt']))
{
    header("location: index.php"); // The user is not logged in. Redirect him to the login page.
}

$page_role="leerling"; // This must be admin for admin.php and student for student.php and similar

$role=$_SESSION['user_role'];

if($role!=$page_role) // If student come to admin page by mistake or admin to student and similar
{
    echo "You are not supposed to be here.";
    die();
}
?>
<!DOCTYPE html>
<html>
    <head>
        <title>Home</title>
        <link rel="stylesheet" href="css/style.css">
        <link rel="stylesheet" href="css/fontello.css">
    </head>

    <body>
        <div class="siteContainer">
            <div class="navLeft">
                <a href="overzicht.php">
                    <img src="../../img/logo.png" alt="HWSysteem" class="mainLogo">
                </a>
            </div>
        </div>
    </body>
</html>
  • 写回答

1条回答 默认 最新

  • duangou2028 2014-04-08 17:49
    关注

    Since I am more comfortable with prepared statements, I have used it here. This is a simple logic example for your requirement.

    <?php
    session_start();
    $mysqli=new MySQLi("localhost", "USER_NAME_HERE", "PASSWORD_HERE");
    $role="";
    
    $username=filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING);
    $password=filter_input(INPUT_POST, 'password', FILTER_SANITIZE_STRING);
    
    if($query=$mysqli->prepare("SELECT `role` FROM members WHERE username=? AND password=?"))
    {
        $query->bind_param("ss", $username, $password);
        $query->execute();
        $query->bind_result($role);
        $query->fetch();
    }
    else
    {
        echo "Errors in the Query. ".$mysqli->error;
        die();
    }
    
        if($role!="")
        {
            $_SESSION['ingelogt']=$username;
            $_SESSION['user_role']=$role;
            $location="$role.php"; // If role is admin this will be admin.php, if student this will be student.php and more.
            header("location: $location"); // Redirect to the respective pages.
        }
        else
        {
            echo "Invalid password, username combination";
        }
    
    ?>
    
    And in your admin.php, student.php
    
    <?php
    if(!isset($_SESSION['ingelogt']))
    {
        header("location: login.php"); // The user is not logged in. Redirect him to the login page.
    }
    
    $page_role="admin"; // This must be admin for admin.php and student for student.php and similar
    
    $role=$_SESSION['user_role'];
    
    if($role!=$page_role) // If student come to admin page by mistake or admin to student and similar
    {
        echo "You are not supposed to be here.";
        die();
    }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决