停留在 "if" 以进行排名检查消息

Stuck at the "if" for rank checking message

我正忙于编写登录脚本,但目前我被 php 代码 "if" 行卡住了。

我想给等级检查一个自己的消息,该用户不被允许,因为他没有适合管理员登录的等级。此时提示用户名或密码错误

我的代码:

<?php
session_start();
include_once 'dbconnect.php';

if(isset($_SESSION['userSession']))
{
header("Location: home.php");
exit;
}

if(isset($_POST['btn-login']))
{
$email = $MySQLi_CON->real_escape_string(trim($_POST['user_email']));
$upass = $MySQLi_CON->real_escape_string(trim($_POST['password']));

$query = $MySQLi_CON->query("SELECT user_id, user_email, user_pass, user_rank FROM users WHERE user_email='$email'");
$row=$query->fetch_array();
if(password_verify($upass, $row['user_pass']) && ($row['user_rank'] == '2'))
{
    $_SESSION['userSession'] = $row['user_id'];
    header("Location: home.php");
}
else
{
    $msg = "<div class='alert alert-danger'>
                <span class='glyphicon glyphicon-info-sign'></span> &nbsp; email or password does not exists!
            </div>";
}

$MySQLi_CON->close();

}
?>

我对PHP还是有点陌生​​。

只需添加一个 else if 阶段。你想吃多少就吃多少,只是不要太过分了。

if(password_verify(...)) {
   ...
} else if ($rank != 2) {
   ... wrong rank 
} else if (...) {
   ...
} else {
   ...
}

只要在密码校验if语句中加入if语句,就可以只校验密码匹配时的排名。

if(isset($_SESSION['userSession']))
{
    header("Location: home.php");
    exit;
}

if(isset($_POST['btn-login']))
{
    $email = $MySQLi_CON->real_escape_string(trim($_POST['user_email']));
    $upass = $MySQLi_CON->real_escape_string(trim($_POST['password']));
    $query = $MySQLi_CON->query("SELECT user_id, user_email, user_pass, user_rank FROM users WHERE user_email='$email'");
    $row = $query->fetch_array();

    if(password_verify($upass, $row['user_pass']))
    {
        if($row['user_rank'] == '2'){
            $_SESSION['userSession'] = $row['user_id'];
            header("Location: home.php");
        } else {
            echo "You need a higher rank";
        }
    }
    else
    {
        $msg = "<div class='alert alert-danger'>
                    <span class='glyphicon glyphicon-info-sign'></span> &nbsp; email or password does not exists!
                </div>";
    }

    $MySQLi_CON->close();
}
?>