简体   繁体   English

登录后如何将用户重定向到他们指定的页面?

[英]How to redirect users to their specified page after logging in?

I've modified the login file to redirect user to their specified page.我修改了登录文件以将用户重定向到他们指定的页面。 But my code simply redirect every user to the first option(rd).但是我的代码只是将每个用户重定向到第一个选项(rd)。 Users under pd department are directed to rd page. pd 部门下的用户被定向到 rd 页面。 My code is as below.我的代码如下。 Note: Please ignore SQL injection comments if there's any vulnerability... My db table, aside from names, includes the columns access level (admin & user) department (rd & pd).注意:如果有任何漏洞,请忽略 SQL 注入评论...我的数据库表,除了名称,还包括列访问级别(管理员和用户)部门(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.";
}
}
?>

I imagine you are also getting errors?我想你也得到错误?

$department=rd is an assignment (to an undefined constant.). $department=rd是一个赋值(对一个未定义的常量)。

$department == 'rd' is a valid comparison. $department == 'rd'是一个有效的比较。

Also, based on your code, $department is undefined.此外,根据您的代码, $department未定义。 You'll want to use $row['department'] instead.你会想要使用$row['department']代替。 And seriously, crank your PHP error reporting up a notch or two, it'll help you heaps.说真的,把你的 PHP 错误报告提高一两个档次,它会帮助你很多。 ;) ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM