php - 登录后如何将用户重定向到他们指定的页面?
问题描述
我修改了登录文件以将用户重定向到他们指定的页面。但是我的代码只是将每个用户重定向到第一个选项(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.";
}
}
?>
解决方案
我想你也得到错误?
$department=rd
是一个赋值(对一个未定义的常量!)。
$department == 'rd'
是一个有效的比较。
此外,根据您的代码,$department
未定义。你会想$row['department']
改用。说真的,把你的 PHP 错误报告提高一两个档次,它会帮助你成堆的。;)
推荐阅读
- c# - NavigationView 中的 UWP 多个 MenuItem 视图
- php - 如何使用将csv文件转换为多维数组的递归函数
- vba - 为什么有时相同的变量声明会产生编译错误?
- java - 动态分配给变量的类类型
- asp.net-core - 使用 DBContext ChangeTracker 编写基于事件的 SignalR 通知服务 - 关注点分离
- java - Java:在连续文件中创建和写入
- react-native - 用 reactNative 中的视图替换特定文本
- hive - 我的 hive 分区策略和视图策略应该是什么,这样查询才能在 10 秒内高效运行并返回结果
- bluetooth - 在适用于 IOS 和 ANDROID 的 Appcelerator Titan 中获取蓝牙访问权限
- triggers - 更新后触发:如何对更新的每一行执行查询