如果输入了错误的电子邮件和密码,则不显示错误消息

时间:2016-03-16 19:02:42

标签: php mysqli login

我有一个登录系统,其中有多个成员类型。我需要这段代码,以便在找不到已输入的电子邮件和密码时显示错误消息。

换句话说,当输入错误的登录详细信息时,我需要它显示错误消息。当输入错误的详细信息时,页面会刷新。

        $check_user = mysqli_num_rows($user_query); // checking to see if there is infact a user which those credentials in the DB
            if ($check_user==1){

            while ($row = mysqli_fetch_array($user_query)){
                    $id = $row['user_id'];
                    $user_type = $row['account'];
                }
                $_SESSION["user_login"] = $login_user;
                // check the user type and redirect according to it
                if($user_type == "Student"){
                 $student_page = "profile_student.php";
                 header( "Location:{$student_page}" );

               } elseif ($user_type == "Landlord"){
                  $landlord_page = "landlord_profile.php";
                  header( "Location:{$landlord_page}" );

               } elseif ($user_type == "Administrator"){
                   $admin_page = "admin_profile.php";
                   header( "Location:{$admin_page}" );

               }else {
                   $refresh_page = "sign_up.php";
               }
                 header( "Location:{$refresh_page}" ); // refresh page
               exit;
            }else {
                echo "<div class='wrong_login'>
                            <p> Email or password is incorrect, please try again. </p>
                    </div>";


            }
    }

        ?>

1 个答案:

答案 0 :(得分:0)

标题后,您必须使用&#39; exit();&#39;否则代码执行继续。

代码执行继续到最后一个重定向标头,这就是所遵循的。

修正了,更容易阅读的代码:

           if($user_type == "Student"){
               $refresh_page = "profile_student.php";

           } elseif ($user_type == "Landlord"){
              $refresh_page = "landlord_profile.php";

           } elseif ($user_type == "Administrator"){
               $refresh_page = "admin_profile.php";

           }else {
               $refresh_page = "sign_up.php";
           }
           header( "Location:{$refresh_page}" ); // refresh page
           exit;

或做

        if($user_type == "Student"){
             $student_page = "profile_student.php";
             header( "Location:{$student_page}" );
             exit();

        } elseif ($user_type == "Landlord"){
              $landlord_page = "landlord_profile.php";
              header( "Location:{$landlord_page}" );
              exit();

        } elseif ($user_type == "Administrator"){
              ...