Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi!

I have created a login form with hashing but when I try to login it always return true even I input a wrong password. Below is my code.

<?php
session_start();
include("../config/db_connection.php");

$un=$_POST['username'];
$pw=$_POST['password'];
$password_hash=password_hash($pw, PASSWORD_BCRYPT);

$stmt = $db->prepare("SELECT password from sys_user WHERE username=? AND password=?");
$stmt->execute(array($un, $pw));
$row_count = $stmt->rowCount();

if (password_verify($pw, $password_hash)) {
    $_SESSION["username"]=$un;
    echo "Correct";
}
else {
    echo "user: " . $un. "<br>";
    echo "pass: " . $password_hash. "<br>";
    echo "Wrong Username or Password";
}
?>


Please help me. Thank you in advance.

What I have tried:

A lot of manipulation to my code.
Posted
Updated 7-Sep-18 4:20am
Comments
Mohibur Rashid 5-Sep-18 21:00pm    
First of all the way you are comparing does not make sense. How had you register a password in the first place. Show us the query please
Kenjiro Aikawa 6-Sep-18 14:51pm    
Hi sir, Below is my code for registering the account.

Kenjiro Aikawa 17-Sep-18 14:34pm    
Hi, I've already solved my problem. Thank you so much for your suggestions.

Look at the documentation for the password_verify function:
PHP: password_verify[^]

You need to pass in the entered password and the stored hash of the original password.

You are passing in the entered password and the computed hash of the entered password. You are totally ignoring the stored hash of the original password.

This is equivalent to checking whether the password that the user typed in is equal to itself. This will obviously always be true!

Read the user record from the database based on the entered username. Read the value of the password column from the database record. Pass that value as the second argument to the password_verify function.


Also, your sign-up code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]
PHP: SQL Injection - Manual[^]
PHP: Prepared statements and stored procedures - Manual[^]
 
Share this answer
 
Comments
Kenjiro Aikawa 17-Sep-18 14:34pm    
Hi, I've already solved my problem. Thank you so much for your suggestions.
<?php
include_once("../config/db_connection.php");

        $password_hash = password_hash($_POST['password'], PASSWORD_BCRYPT);
        $sql = "INSERT INTO sys_user (username, password, account_type)
         
        VALUES ('".$_POST["username"]."', '$password_hash', '".$_POST["accounttype"]."')";
        ($db->query($sql));
        header("Location: user_account.php?msg=correct");
?>
 
Share this answer
 
Comments
Richard Deeming 7-Sep-18 10:14am    
DO NOT post comments using the "Add your SOLUTION here" box!
Kenjiro Aikawa 9-Sep-18 19:05pm    
Thank you so much for you reply sir. I'll review my code.
Kenjiro Aikawa 17-Sep-18 14:33pm    
Hi, I've already solved my problem. Thank you so much for your suggestions.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900