【发布时间】:2021-12-03 09:11:08
【问题描述】:
我能够成功创建会话并运行它们,并且它在那些特定用户专有的页面中运行良好。
例如,如果有 8 个管理页面,那么这 8 个页面将包含仅检查来自管理类的管理会话的代码。
但是,如果有一个页面,所有三个都具有用户权限,这将是登录页面,我无法为三个单独的用户一起处理三个会话。当我已经使用任何一个用户登录到系统然后尝试转到登录页面时,它应该会自动将我重定向到该用户的专有主页。但它反而要求我再次输入登录凭据。
我的 PHP 代码:
<?php
if(isset($_SESSION['p_login'])){
header("location:login_tests/Patient.php");
}
else if(isset($_SESSION['d_login'])){
header("location:login_tests/Doctor.php");
}
else if(isset($_SESSION['a_login'])){
header("location:admin/Dashboard.php");
}
// Login functionalities
if(isset($_POST['submit1'])){
// Patient login
if($_POST['users']=="patient"){
session_start();
include_once 'classes/patient.php';
$patient = new Patient();
if ($patient->p_session())
{
header("location:login_tests/Patient.php");
}
$patient = new Patient();
if ($_SERVER["REQUEST_METHOD"] == "POST"){
$login = $patient->p_login($_REQUEST['username'],$_REQUEST['password']);
if($login){
header("location:login_tests/Patient.php");
}
else
{
echo "<script>alert('Login Failed!');</script>";
}
}
}
// Doctor login
else if($_POST['users']=="doctor"){
session_start();
include_once 'classes/doctor.php';
$doctor = new Doctor();
if ($doctor->d_session())
{
header("location:login_tests/Doctor.php");
}
$doctor = new Doctor();
if ($_SERVER["REQUEST_METHOD"] == "POST"){
$login = $doctor->d_login($_REQUEST['username'],$_REQUEST['password']);
if($login){
header("location:login_tests/Doctor.php");
}
else
{
echo "<script>alert('Login Failed!');</script>";
}
}
}
// Admin login
else{
session_start();
include_once 'classes/admin.php';
$admin = new Admin();
if ($admin->a_session())
{
header("location:admin/Dashboard.php");
}
$admin = new Admin();
if ($_SERVER["REQUEST_METHOD"] == "POST"){
$login = $admin->a_login($_REQUEST['username'],$_REQUEST['password']);
if($login){
header("location:admin/Dashboard.php");
}
else
{
echo "<script>alert('Login Failed!');</script>";
}
}
}
}
// Registration functionalities
else if(isset($_POST['submit2'])){
include_once 'classes/patient.php';
$patient = new Patient();
if ($_SERVER["REQUEST_METHOD"] == "POST"){
$register = $patient->p_register($_REQUEST['name'],$_REQUEST['gender'], $_REQUEST['dob'], $_REQUEST['nic'], $_REQUEST['address'],$_REQUEST['email'], $_REQUEST['username'], $_REQUEST['password']);
if($register){
echo "<script>alert('Registration Successful!');</script>";
}
else
{
echo "<script>alert('Entered email address or username already exists!');</script>";
}
}
}
?>
【问题讨论】:
-
我投票结束这个问题,因为代码示例太长了。请edit您的帖子将您的代码缩减为minimal, complete, readable, and reproducible example。
标签: php authentication session permissions