PHP 从通过类调用的 If/Else If 语句返回值


PHP Returning value from If/Else If statement called via class

我试图弄清楚为什么这不起作用。我正在尝试做的是在调用函数后返回一个值。它是一个登录功能,应该检查数据库中的用户状态。该值$userStatus。根据状态,我想返回一个值,然后触发错误。但是,脚本仅触发第一个 else if 语句。

这是调用脚本的索引页

if(isset($_POST['btn-login'])) {
    $uname = strip_tags($_POST['txt_uname_email']);
    $umail = strip_tags($_POST['txt_uname_email']);
    $upass = strip_tags($_POST['txt_password']);
    if ($login->doLogin($uname,$umail,$upass)) {
        $login->userLoginTime($uname);
        $login->redirect('home.php');
    } else if ($userStatus == 0) {
        $error = "Your account is not active!";
    } else if ($userStatus == "") {
        $error = "You don't have an account, please sign up";
    }
}
?>

这是容纳该函数的类页面。

public function doLogin($uname,$umail,$upass)
{
    try 
    {
        $stmt = $this->conn->prepare("SELECT
            user_id, user_name, user_email, user_pass, Enabled
            FROM users WHERE user_name=:uname OR user_email=:umail ");
        $stmt->execute(array(':uname'=>$uname, ':umail'=>$umail));
        $userRow=$stmt->fetch(PDO::FETCH_ASSOC);
        $userStatus = $userRow['Enabled'];
        if ($userStatus == 5) {
            if ($stmt->rowCount() == 1) {
                if (password_verify($upass, $userRow['user_pass'])) {
                    $_SESSION['user_session'] = $userRow['user_id'];
                    return true;
                }
            } else {
                return false;
            }
        }
        if ($userStatus == 0) {
            return $userStatus;
        }
        if ($userStatus == "") {
            return $userStatus;
        }
    }

$userStatus永远不会被设置。 您需要在if之前设置它:

$userStatus = $login->doLogin($uname,$umail,$upass);
if($userStatus === true) {       
   $login->userLoginTime($uname);
   $login->redirect('home.php');
} else if ($userStatus === 0) {
   $error = "Your account is not active!";
} else if ($userStatus === "") {
   $error = "You don't have an account, please sign up";
} 

中的"txt_uname_email"

$uname = strip_tags($_POST['txt_uname_email']);

不正确吗?有名字的东西?