1

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;
        }
    }
3
  • do you create an instance of a class somewhere? Commented Mar 7, 2016 at 23:45
  • Yes I do have an instance of the class at the very top of the page, sorry for not including it. Commented Mar 7, 2016 at 23:46
  • Yes I do have a catch block, but there is no error. Commented Mar 7, 2016 at 23:51

2 Answers 2

5

$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";
} 
Sign up to request clarification or add additional context in comments.

5 Comments

I will try this. Does this still apply even though the user status is being set in the class?
The class and your code calling it are in different scopes. Data doesn't pass freely between them. return $userStatus in your class is saying, "Hey, I'm done and here is this data you can use," but the code calling it never takes that data and stores it anywhere, so it gets lost.
I think it would be a good idea to use === instead of ==. That last condition may never be reached.
Thank you very much. I'll try this and read up on it using the link you've provided. If it works I'll be sure to make it as the answer
@Andrew - Right on. They're both falsy values.
1

maybe "txt_uname_email" in

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

is incorrect? Something with name?

3 Comments

I am able to login using the function if my user name and password are correct. However if my password is wrong I want it to say incorrect password. And when the password is right it should check the usersstatus and if not active it should echo user not active
However the only error i receive is user not active although it is my password that is wrong
You put the posted email in the $uname variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.