如何根据角色重定向两个用户?我有数据库用户类型1和类型0.我的登录代码是这样的。类型1假定为管理员,类型0是普通用户。
<?php
session_start();
if(isset($_POST['submit'])){
include 'dbh.inc.php';
$uid = mysqli_real_escape_string($conn, $_POST['uid']);
$pwd = mysqli_real_escape_string($conn, $_POST['pwd']);
if (empty($uid) || empty($pwd)){
header("Location: ../index.php?login=empty");
exit();
} else {
$sql = "SELECT * FROM users WHERE user_uid='$uid'";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck < 1) {
header("Location: ../index.php?login=error");
exit();
} else {
if ($row = mysqli_fetch_assoc($result)){
$hashedPwdCheck = password_verify($pwd, $row['user_pwd']);
if($hashedPwdCheck == false){
header("Location: ../index.php?login=error");
exit();
} elseif ($hashedPwdCheck == true) {
$_SESSION['u_id'] = $row['user_id'];
$_SESSION['u_first'] = $row['user_first'];
$_SESSION['u_last'] = $row['user_last'];
$_SESSION['u_uid'] = $row['user_uid'];
header("Location: ../main.php?login=success");
exit();
}
}
}
}
} else {
header("Location: ../index.php?login=error");
exit();
}
答案 0 :(得分:0)
您可以通过if()..else...
轻松完成此操作,但完全不要直接使用header
重定向,我建议您使用此功能重定向:
function redirect($url){
if (headers_sent()){
die('<script type="text/javascript">window.location.href=\'' . $url . '\';</script>');
}else{
header('Location: ' . $url);
die();
}
}
这是你需要做的:
if($type == 1)
redirect($admin_url);
else
redirect($user_url);
答案 1 :(得分:-1)
您只是通过查询检查用户类型是1还是0?
while ($row = mysqli_fetch_assoc($result)) {
$type = $row["yourusertypeid"];
}
if ($type == 1) {
//user is admin
} else if ($type == 1) {
//user is user
}