douzuan5365 2013-03-07 19:01
浏览 37
已采纳

禁止和用户激活系统,但我在登录脚本上有一些问题

I'm new to php. I have a login system, and now I'm trying to implement a ban and user activation system but I have some problems on the login script. Here is the code from my script:

 <?php
    $query = "SELECT id, username, password, salt, email, firstname, lastname, active, banned FROM users WHERE username = :username "; 
    $query_params = array( 
        ':username' => $_POST['username'] 
    ); 
    try 
    { 
        $stmt = $db->prepare($query); 
        $result = $stmt->execute($query_params); 
    } 
    catch(PDOException $ex) 
    { 
        die("Failed to run query: " . $ex->getMessage()); 
    } 
    $row = $stmt->fetch(); 

    $login_ok = false; 
    $login_match = false; 
    $login_active = false; 
    $login_banned = false; 

    if($row) 
    { 
        $check_password = hash('sha256', $_POST['password'] . $row['salt']); 
        if($check_password === $row['password']) 
        { 
            $login_match = true; 
        } 
    if($row['active'] == 1) {
        $login_active = true;
    }
    if($row['banned'] == 1) {
        $login_banned = true;
    }
    if($login_match && $login_active && !$login_banned) {
        $login_ok = true;
    }
    } 
    if($login_ok) 
    { 
        unset($row['salt']); 
        unset($row['password']); 
        $_SESSION['user'] = $row; 
        header("Location: index.php"); 
        die("Redirecting..."); 
    } 
    else 
    { 
        if(!$login_match) { echo "Wrong username/pasword.";}
        if(!$login_active) { echo "Account not activated, check your email";}
        if($login_banned) { echo "Your account is banned";}
    } 
?>

In my Database I have 2 columns active and banned, where 0 means that account is activated and not banned, and 1 if account is not activate or is banned.

How can I display different messages to the user? If a user will enter a wrong username or password, he will get all three messages from the final else {}. I want to display messages to the user like this: If username or password is wrong, display only Wrong username/pasword. and ignore $login_active $login_banned. If username/password is ok, but account not activated, Account not activated, check your email. and ignore the $login_banned switch. If username/password is ok, but account is banned display Your account is banned and ignore the $login_active switch.

I'm sorry if I wrote too much, I hope I explained right.

  • 写回答

1条回答 默认 最新

  • doude1917 2013-03-07 19:34
    关注

    Change this:

    if(!$login_match) { echo "Wrong username/pasword.";}
    if(!$login_active) { echo "Account not activated, check your email";}
    if($login_banned) { echo "Your account is banned";}
    

    To this:

    <?php
    if ($login_banned == true) {
            echo "Your account is banned";
    } else if ($login_match != true) {
            echo "Wrong username/password.";
    } else if ($login_active != true) {
            echo "Account not activated, check your email";
    }
    ?>
    

    I hope it does what you want.

    Below are options you could still use:

    //PICK OPTIONS DEPENDING ON YOUR PREFERENCE AND MESSAGE PRIORITIES
        //option 1
        if ($login_match != true) {
            echo "Wrong username/pasword.";
        } else if ($login_banned != true) {
            echo "Your account is banned";
        } else if ($login_active != true) {
            echo "Account not activated, check your email";
        }
    
        //option 2
        if ($login_match != true) {
            echo "Wrong username/pasword.";
        } else if ($login_active != true) {
            echo "Account not activated, check your email";
        } else if ($login_banned != true) {
            echo "Your account is banned";
        }
    
        //option 3
        if ($login_banned == true) {
            echo "Your account is banned";
        } else if ($login_match != true) {
            echo "Wrong username/password.";
        } else if ($login_active != true) {
            echo "Account not activated, check your email";
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集