Unable to Log In Despite Correct Username and Password

  Kiến thức lập trình

I’m facing an issue with my login functionality. Even though I provide the correct username and password, the system keeps rejecting my login attempts. Here are the details:

  • I’ve verified that the username and password are accurate.
  • The login form seems to work fine, but the authentication process fails consistently.
  • I’ve checked my database records, and the credentials match what I’m entering.

My index.php

<?php include('header.php'); ?>
  <body id="login">
    <div class="container">

      <form id="login_form" class="form-signin" method="post">
        <h3 class="form-signin-heading"><i class="icon-lock"></i> Please Login</h3>
        <input type="text" class="input-block-level" id="username" name="username" placeholder="Username" required>
        <input type="password" class="input-block-level" id="password" name="password" placeholder="Password" required>
        <button name="login" class="btn btn-info" type="submit"><i class="icon-signin icon-"></i> Sign in</button>
        
              </form>
                <script>
            jQuery(document).ready(function(){
            jQuery("#login_form").submit(function(e){
                    e.preventDefault();
                    var formData = jQuery(this).serialize();
                    $.ajax({
                        type: "POST",
                        url: "login.php",
                        data: formData,
                        success: function(html){
                        if(html=='true')
                        {
                        $.jGrowl("Welcome to School LMS", { header: 'Access Granted' });
                        var delay = 2000;
                            setTimeout(function(){ window.location = 'dashboard.php'  }, delay);  
                        }
                        else
                        {
                        $.jGrowl("Please Check your username and Password", { header: 'Login Failed' });
                        }
                        }
                        
                    });
                    return false;
                });
            });
            </script>

        


    </div> <!-- /container -->
<?php include('script.php'); ?>
  </body>
</html>

Here my login.php

<?php
        include('dbcon.php');
        session_start();
        $username = $_POST['username'];
        $password = $_POST['password'];

        $query = mysqli_query($conn,"SELECT * FROM users WHERE username='$username' AND password='$password'")or die(mysqli_error());
        $count = mysqli_num_rows($query);
        $row = mysqli_fetch_array($query);


        if ($count > 0){
        
        $_SESSION['id']=$row['user_id'];
        
        echo 'true';
        
        mysqli_query($conn,"insert into user_log (username,login_date,user_id)values('$username',NOW(),".$row['user_id'].")")or die(mysqli_error());
         }else{ 
        echo 'false';
        }   
                
        ?>

Could someone please guide me on how to troubleshoot this issue? Is there anything I might be missing in my code or configuration?

Thank you in advance for your assistance!

New contributor

schelen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT