我正在开发一个学校系统(只是为了学习 PHP),我已经完成了管理员端,但我希望我的登录表单接受来自学生、老师和系统管理员的登录数据,以便我可以显示每种类型用户的系统特定 View 。在“if(isset($_POST["log"]))”下注释的代码是我当前为管理员使用的代码,并且它运行完美(按照我的标准)。这是代码:
<?php
//Fazer login para admin e prof;ligar todas databases ao login;????
include("conect.php");
include("classes/class_est.php");
include("classes/class_login.php");
include("classes/class_admin.php");
include("classes/class_prof.php");
$user=(empty($_POST["username"])? "" :$_POST["username"]);
if(isset($_POST["log"])){
/*if(($user)&&($_POST["senha"]!=null)){
$est=new admin();
$est->login($user,$_POST["senha"],$_POST["tipo_user"]);
}else{
echo"<script>alert('Nome ou Senha Incorrecto')</script>";
}
}*/
switch($_POST["tipo_user"]){
case"administrador":echo 'ighb';
if(($_POST["username"])&&($_POST["senha"]!=null)){
$admin=new admin();
$admin->login($_POST["username"],$_POST["senha"],$_POST["tipo_user"]);
}else{
echo"<script>alert('Nome ou Senha Incorrectos')</script>";
}
;
break;
case"estudante":
if(($_POST["username"])&&($_POST["senha"]!=null)){
$est=new est();
$est->login($_POST["username"],$_POST["senha"],$_POST["tipo_user"]);
}else{
echo"<script>alert('Nome ou Senha Incorrectos')</script>";
};
break;
case"professor":
if(($_POST["username"] && $_POST["senha"])!=null){
$prof=new prof();
$prof=login($_POST["username"],$_POST["senha"],$_POST["tipo_user"]);
}else{
echo"<script>alert('Nome ou Senha Incorrectos')</script>";
};
break;
default;break;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="estilos/forms.css"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<center>
<div id="login">
<div id="black_m"><img src="estilos/imagens/black_mamba_logo.png"/></div>
<table>
<form method="POST">
<tr><td>Tipo de usuário:</td><td><?php $listar=new login;$listar->list_tipo()?></td><td><span>(Para Alpha)</span></td></tr>
<tr><td>Username: </td><td><input type="text" name="username" placeholder="Insira o nome de usuário" id="texto"/></td></tr>
<tr><td>Senha: </td><td><input type="password" name="senha" placeholder="Insira a senha" id="texto"/><td></tr>
<tr><td><input type="submit" name="log" value="ENTRAR" id="texto"/></tr>
</form>
</table>
</div>
</center>
</body>`
最佳答案
您熟悉数据库的使用吗?将用户名、密码和 access_levels 存储在数据库的用户表中可能是您的最佳选择。否则,您将必须在脚本中对用户的访问值进行硬编码。如果这样做,您可以创建一个定义其权限值的开关,例如:
$username=$_POST['user'];
switch $username {
case "supercool_admin":
$access_level="admin";
break;
case "supercool_student":
$access_level="student";
break;
case "supercool_teacher":
$access_level="teacher";
break;
default:
$access_level="none";
}
然后,当您显示内容时,您可以将 access_level 作为 if 语句或另一个开关引用。
例如:
if($access_level=="admin") {
//DISPLAY ADMIN CONTENTS
} elseif ($access_level=="teacher") {
//DISPLAY TEACHER CONTENTS
} elseif ($access_level=="student") {
//DISPLAY STUDENT CONTENTS
} else {
echo "YOU DO NOT HAVE PERMISSION TO VIEW THESE CONTENTS";
}
我还建议不要使用后值进行查找,而是使用后值对数据库进行身份验证,或者在您的情况下,在交换机内进行身份验证。经过身份验证并定义其 access_level 后,将这些值存储在 session 中并引用该 session 。
关于php - php中的用户权限(和多用户登录表单),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10921633/