首页 > 解决方案 > 如何添加我的代码检查第三人而不是用户和管理员,如图所示

问题描述

在这段代码中,我将展示它将检查我的数据库天气,或者用户是管理员还是用户。我想为高级用户添加高级。

我试过|| 和不同的 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。然后重新加载。

标签: phpmysql

解决方案


只需通过添加一个单独的 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");


推荐阅读