首页 > 解决方案 > 登录后如何将用户重定向到他们指定的页面?

问题描述

我修改了登录文件以将用户重定向到他们指定的页面。但是我的代码只是将每个用户重定向到第一个选项(rd)。pd 部门下的用户被定向到 rd 页面。我的代码如下。注意:如果有任何漏洞,请忽略 SQL 注入注释...我的 db 表除了名称之外,还包括列访问级别(admin & user)部门(rd & pd)。

<?php
if(!isset($_SESSION)){
 session_start();
                     }
include_once("connections/connection.php");
$con = connection();

if(isset($_POST['login'])){

$username = $_POST['username'];
$password = $_POST['password'];

$sql = "SELECT * FROM users_table WHERE username = '$username' AND password = '$password'";

$user = $con->query($sql) or die ($con->error);
$row = $user->fetch_assoc();
$total =$user->num_rows;

if($total > 0 AND $department=rd){
$_SESSION['UserLogin'] = $row['username'];
$_SESSION['Access'] = $row['access'];
$_SESSION['Fname'] = $row['fname'];
$_SESSION['Lname'] = $row['lname'];
$_SESSION['Department'] = $row['department'];

echo $_SESSION['UserLogin'];
echo header("Location: index_rd.php");}

else  if($total > 0 AND $department=pd){
$_SESSION['UserLogin'] = $row['username'];
$_SESSION['Access'] = $row['access'];
$_SESSION['Fname'] = $row['fname'];
$_SESSION['Lname'] = $row['lname'];
$_SESSION['Department'] = $row['department'];

echo $_SESSION['UserLogin'];
echo header("Location: index_proc.php");}

else{
echo "No user found.";
}
}
?>

标签: phpif-statementauthenticationsession-variablesforms-authentication

解决方案


我想你也得到错误?

$department=rd是一个赋值(对一个未定义的常量!)。

$department == 'rd'是一个有效的比较。

此外,根据您的代码,$department未定义。你会想$row['department']改用。说真的,把你的 PHP 错误报告提高一两个档次,它会帮助你成堆的。;)


推荐阅读