php - 如何添加我的代码检查第三人而不是用户和管理员,如图所示
问题描述
在这段代码中,我将展示它将检查我的数据库天气,或者用户是管理员还是用户。我想为高级用户添加高级。
我试过|| 和不同的 else 语句。我已经搜索过了,无法修复。请帮助我,因为我真的需要帮助。
这现在是当前的代码
if (mysqli_num_rows($results) == 1) { // user found
// check if user is admin or user
$logged_in_user = mysqli_fetch_assoc($results);
if ($logged_in_user['user_type'] == 'admin') {
$_SESSION['user'] = $logged_in_user;
$_SESSION['success'] = "You are now logged in";
header('location: admin/home.php');
}else{
$_SESSION['user'] = $logged_in_user;
$_SESSION['success'] = "You are now logged in";
header('location: index.php');
}
}else {
array_push($errors, "Wrong username/password combination");
}
}
}
我试过了
if (mysqli_num_rows($results) == 1) { // user found
// check if user is admin or user
$logged_in_user = mysqli_fetch_assoc($results);
if ($logged_in_user['user_type'] == 'admin' || 'prem') {
$_SESSION['user'] = $logged_in_user;
$_SESSION['success'] = "You are now logged in";
header('location: admin/home.php');
}else{
$_SESSION['user'] = $logged_in_user;
$_SESSION['success'] = "You are now logged in";
header('location: index.php');
}
// a else statement goes here I forgot what I put but it was simalr to lines above.
}else {
array_push($errors, "Wrong username/password combination");
}
}
}
当我进入它。输出应该是我的页面。输出真的只是在说那个悲伤的错误脸。错误 HTTP 500。然后重新加载。
解决方案
只需通过添加一个单独的 else 循环来检查“高级”用户类型来扩展您已经拥有的内容。(见下文)
if ($logged_in_user['user_type'] == 'admin') {
session_start();
$_SESSION['user'] = $logged_in_user;
$_SESSION['success'] = "You are now logged in";
exit(header("Location: admin/home.php");
} elseif ($logged_in_user['user_type'] == 'prem') {
session_start();
$_SESSION['user'] = $logged_in_user;
$_SESSION['success'] = "You are now logged in";
exit(header("Location: premiumindex.php");
} else {
session_start();
$_SESSION['user'] = $logged_in_user;
$_SESSION['success'] = "You are now logged in";
exit(header("Location: userindex.php");
}
注意:我添加session_start();
到您的循环中以启动会话处理。另外,我被教导将标头重定向编写为:exit(header("Location: page");
推荐阅读
- python - 如何更改 discord-py-slash 命令的顺序
- android - Android 外部存储权限即将发生的变化
- r - 如何在 Rstudio 中使用 grep 和 fread?
- matplotlib - 为新闻散布图添加标签
- javascript - 如何使我的随机图像选择器从随机选择变为顺序选择?
- javascript - 如何将控制台日志结果传递给我的 ajax,以便我可以将其插入 DB?
- html - 通过浏览器直接访问时 index.html 文件不显示样式
- elasticsearch - 如何在 Kibana 发现页面中添加时间范围过滤器?
- c# - 根据最常找到的关键字过滤列表
- typescript - 基于函数类型的打字稿条件类型