【问题标题】:Error message not showing if the user enters incorrect information如果用户输入的信息不正确,则不会显示错误消息
【发布时间】:2016-03-25 12:19:39
【问题描述】:

如果用户没有输入正确的登录组合,我会尝试在最后显示错误。但是,当我输入错误的密码或电子邮件时,不会显示错误消息。任何建议

 <?php
 include ("connect.php");

if (isset($_POST["user_login"]) && (isset($_POST["user_pass"]))){
    // formatting field via reg replace to ensure email and password only conisists of letters and numbers preg_replace('#[^A-Za-z0-9]#i','', 
    $login_user = $_POST["user_login"];
    $login_password = $_POST["user_pass"];


    // password is encryted in DB (MD5) therefore user inputted password will not match encryted password in DB - we have to assign new var
    $decrypted_password = md5($login_password);

// Query which finds user (if valid) from DB - Achieving authentication via username and password       

$user_query = mysqli_query($connect, "SELECT * FROM users WHERE email = '$login_user' AND password = '$decrypted_password' AND closed = 'no' LIMIT 1"); 

    $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

            echo "You have entered an incorrect email or password. Please try again.";

           }
           }
}

    ?>

【问题讨论】:

  • 正确的缩进应该清楚问题是什么。另请注意,您不太可能看到重定向到另一个页面后出现的任何内容。
  • @jeroen 我试图只显示错误消息,但也不起作用

标签: php mysqli login


【解决方案1】:

如果输入数据错误,您将重定向用户,然后您才尝试回显消息,这不是它的工作原理。阅读php_manual 中的标题。可能这里最好的方法是将错误消息存储在会话中,并在重定向后检查会话错误消息是否存在

else {
           $refresh_page = "sign_up.php";
            $_SESSION['error'] = "your error message"
           header( "Location:{$refresh_page}" ); // refresh page
       }

在 sign_up.php 文件中检查会话中是否存在错误消息

if(isset($_SESSION["error"])){
     echo $_SESSION["error"];
      unset($_SESSION["error"]);
  }

也许您应该稍微更正一下这段代码)) 在显示应该从会话中删除的消息后使用未设置原因',在其他情况下,如果您失败例如 5 次,它将显示 5 条消息))还确保会话已启动 session_start() 希望它有帮助:)

【讨论】:

    【解决方案2】:

    只有当$user_type 与您的任何预期类型都不匹配时,您才会显示错误。

    if ($check_user==1){ 块之后,您需要第二个else 来处理使用该电子邮件或密码的用户不存在的情况。

    【讨论】:

      猜你喜欢
      • 2020-03-19
      • 2016-04-02
      • 1970-01-01
      • 2015-07-03
      • 1970-01-01
      • 2016-06-27
      • 1970-01-01
      • 2016-06-25
      • 2016-07-08
      相关资源
      最近更新 更多