简体   繁体   中英

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

I am trying to figure out why this isn't working. What I am trying to do is return a value after the function is called. It is a login function which is supposed to check the database for the users status. That value being $userStatus. Depending on the status I'd like to return a value which would then trigger the error. However the script only fires off the first else if statement.

Here is the index page which calls the script

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";

    }
}
?>

Here is the class page where the function is housed.

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 is never being set. You'll want to set it before your 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";
} 

maybe "txt_uname_email" in

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

is incorrect? Something with name?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM