doudeng2016 2016-03-07 23:39
浏览 47
已采纳

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

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;
        }
    }
  • 写回答

2条回答 默认 最新

  • dongta5621 2016-03-07 23:48
    关注

    $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";
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号