首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用所有不同的重定向页面创建登录会话

使用所有不同的重定向页面创建登录会话
EN

Stack Overflow用户
提问于 2014-04-08 17:19:13
回答 1查看 2.8K关注 0票数 1

)我正在开发一个有3个用户(管理员、教师和学生)的登录系统,所有这些用户都有自己的功能和界面。如果可能的话,我想让所有这些用户一起登录one,我不确定。

我的数据库中有一个行角色(‘管理’,‘教师’,‘学生’),我希望基于“角色”直接登录,所以管理员将被重定向到管理主页,教师重定向到教师主页,学生将重定向到学生主页。行政网页只能由管理员浏览,教师页面只能由教师浏览,学生页面只能由学生浏览。你大概明白我的意思了。

问题:我如何使登录,以便它将检查它是否是管理员/教师/学生?当他们登录时,我如何才能使该用户的特定主页仅由该类型的用户提供?

代码语言:javascript
复制
<?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";
    }

?>

对于userpage,一个例子

代码语言:javascript
复制
<?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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-08 17:49:23

由于我对事先准备好的陈述比较满意,所以我在这里使用了它。这是您的需求的一个简单逻辑示例。

代码语言:javascript
复制
<?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();
}

?>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22943975

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档