gpt4 book ai didi

php - 使用所有不同的重定向页面进行登录 session

转载 作者:行者123 更新时间:2023-11-29 06:38:34 25 4
gpt4 key购买 nike

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

我的数据库中有一行 Role('Admin','Teacher','Student') 我希望根据“角色”直接登录,这样管理员将被重定向到管理员主页,老师将被重定向到教师主页和学生主页。管理页面只能由管理员查看,教师页面只能由教师查看,学生页面只能由学生查看。您可能明白我的意思。

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

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

?>

和用户页面的例子

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

最佳答案

因为我更习惯使用准备好的语句,所以我在这里使用了它。这是满足您要求的简单逻辑示例。

<?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();
}

?>

关于php - 使用所有不同的重定向页面进行登录 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22943975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com