将用户重定向到其仪表板的简单方法


A simple way to redirect users to their dashboards

所以作为一个PHP的新手,我试图找到一种方法,让某些用户进入他们各自的页面。

例如,我希望管理员(登录后)被重定向到他们的管理页面,除了管理员之外,没有其他人应该看到这个页面

,以及应该被重定向到他们的导师页面的导师,除了导师之外没有其他人可以看到这个。

不幸的是,我

似乎无法思考如何使这项工作如此工作,并且我环顾四周,看到其他示例,其中很多与我正在寻找的内容无关,因为其中很多都是关于"权限"的。

这是我的登录页面代码:

<?php
//error_reporting(0);
error_reporting(-1);
ob_start();
session_start();
//connection to the database
require 'connection.php';
if (!empty($_POST['username']) && !empty($_POST['password']))
{
  //query the databse for these columns in the table users
  $records = $conn->prepare('SELECT id, Username, Password, Role FROM users WHERE Username = :username');
  $records->bindParam(':username', $_POST['username']);
  $records->execute();
  $results = $records->fetch(PDO::FETCH_ASSOC);
  //var_dump($results);
  // Count the results in the table if they are greater than 0
  // check the hashed password on the form and the password in the db match
  if((count($results) > 0) && password_verify($_POST['password'], $results['Password'])) {
    //die('We have a log in');
    if ($results == 'Admin'){
      header('Location: adminPage.php');
    } else if ($results == 'Tutor'){
      header('Location: tutorPage.php');
    } else {
      header('Location: studentPage.php');
    }
  } else {
    //echo $_POST['password'];
    die('That user doesn''t exist');
  }
}
?>

在尝试执行代码之后,当然会出现一些错误:

[:error] [pid 1797] [client 127.0.0.1:37161] PHP Parse error:  syntax error, unexpected '''' (T_CONSTANT_ENCAPSED_STRING) in /apps/bla/web/login.php on line 31, referer: http://hinat.local/loginPage.php

尝试执行此操作时,我需要考虑什么,或者我可以这样做的最简单方法是什么?

在测试角色时,您需要将$results['Role']作为数组进行寻址,最好在header('Location: ....);后添加exit;,因为标头命令不会停止代码执行。

  if((count($results) > 0) && password_verify($_POST['password'], $results['Password'])) {
    //die('We have a log in');
    if ($results['Role'] == 'Admin'){
      header('Location: adminPage.php');
      exit;
    } else if ($results['Role'] == 'Tutor'){
      header('Location: tutorPage.php');
      exit;
    } else {
      header('Location: studentPage.php');
      exit;
    }
  } else {
    //echo $_POST['password'];
    die('That user doesn''t exist');
  }