1

I've been trying to solve the problem myself to no avail. Below is my code:

checklogin.php

<?php
session_start();
// Connect to db
include('db.php');

$stmt = $conn->prepare('SELECT * FROM users WHERE username = :username AND password = :password');
$stmt->execute(array(':username' => $_POST['username'], ':password' =>   $_POST['password']));

$result = $stmt->fetch(PDO::FETCH_ASSOC);
$affected_rows = $stmt->rowCount();
//$stmt->setFetchMode(PDO::FETCH_ASSOC);
//$affected_rows = $stmt->fetchColumn();

$username = $_POST['username'];
$password = $_POST['password'];

/*
if($affected_rows == 1) {
    /*while($affected_rows = $result){
        //add the user to our session variables
        $_SESSION['username'] = $username;
        if($affected_rows['authority'] == 'admin'){
            $_SESSION['authority'] == 'admin';
            header("Location: main.php");
            exit;
        }
        if($affected_rows['authority'] == 'doctor'){
            $_SESSION['authority'] == 'doctor';
            header("Location: pages/doctor/index.php");
            exit;
        }
        if($affected_rows['authority'] == 'nurse'){
            $_SESSION['authority'] == 'nurse';
            header("Location: pages/nurse/index.php");
            exit;
        }
    }
    echo 'OK';
}

else if($username == '' || $password == '' ){
    echo 'Blank fields!';
}
else {
    print 'access is not allowed !!!';
} // end of $affected_rows
*/

    if($affected_rows > 0){
        echo "Yes";
    }
    else{
        echo "No";
    }

 ?>

index.php

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script>
<script language="javascript">  
    $(document).ready(function(){  
        $("#loginForm").submit(function(){  

            $("#report").removeClass().addClass('loader').html('<img src="images/ajax-loader.gif">').fadeIn(1000);  
            $.post("checklogin.php",{ user_name:$('#username').val(),password:$('#password').val()},function(data){  
                if(data=='yes'){  
                    $("#report").fadeTo(200,1,function(){       
                        $(this).html('Logging in.....').addClass('log').fadeTo(900,1,function(){          
                            document.location='main.php';  
                        });       
                    });  
                } else {  
                    $("#report").fadeTo(200,1,function(){        
                        $(this).html('Username or password error.').addClass('error').fadeTo(900,1);  
                    });    
                }  
            });  
            return false;   
        });  

        $("#password").blur(function(){  
            $("#loginForm").trigger('submit');  
        });  
    });  
</script>  


</head>

<body>

<div class="container">
  <form class="form-signin" action="checklogin.php" method="post" id="loginForm">
    <h2 class="form-signin-heading">User Login</h2>
    <input type="text" class="input-block-level" placeholder="Username" name="username" id="username">
    <input type="password" class="input-block-level" placeholder="Password" name="password" id="password">
    <label class="checkbox">
      <input type="checkbox" value="remember-me"> Remember me
    </label>
    <button class="btn btn-large btn-primary" type="submit" name="login" id="login">Sign in</button> 

    <div id="report"></div>

  </form>
</div> <!-- /container -->

When I input the wrong, it gives Username or password error, but when I input the correct it gives me Username or password error. Any ideas? Thanks.

2
  • Is the password really stored as plaintext or should you be hashing it for the comparison?? Commented Jul 4, 2013 at 18:28
  • Hmm i think the latter, I'm a beginner so I don't know how to hash it. Commented Jul 4, 2013 at 18:34

4 Answers 4

2

First problem is in the ajax call, you're setting:

user_name:$('#username').val()

That should be with no underscore, such as:

username:$('#username').val()

Second problem is you need to change your yes comparison to Yes:

if(data=='Yes'){ 
Sign up to request clarification or add additional context in comments.

Comments

0

"PDOStatement::rowCount() returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement executed by the corresponding PDOStatement object.

If the last SQL statement executed by the associated PDOStatement was a SELECT statement, some databases may return the number of rows returned by that statement. However, this behaviour is not guaranteed for all databases and should not be relied on for portable applications."

As stated here: http://php.net/manual/de/pdostatement.rowcount.php

Is guess this might be your error.

EDIT: Instead try to COUNT(*) the rows fitting.

Another solution could be: https://stackoverflow.com/a/2304377/1386873

Comments

0

I think you need to change the following line:

function(data) { if data = 'yes' 

to

function(data) { if data = 'Yes' 

Comments

0

may be you must add $.trim(), for trim your data before you send to server, such as :

user_name: $.trim($('#username').val())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.