Why is it showing incorrent password
after comparing two equal md5 keys?
<?php
if (isset($_POST['user_password']) && !empty($_POST['user_password'])) {
$user_password = $_POST['user_password'];
echo $user_passkey = md5($user_password).'<br>';
$filename = 'hash.txt';
$handle = fopen($filename, 'r');
echo $file_password = fread($handle, filesize($filename));
if ($user_passkey==$file_password) {
echo 'correct password';
} else {
echo 'Incorrect Password';
}
} else {
echo 'Please enter a password';
}
?>
<form action="index.php" method="POST">
Password:
<input type="text" name="user_password"><br><br>
<input type="submit" value="Submit">
</form>
The other md5 created file is :
<?php
$string = 'password';
$string_hash = md5($string);
echo $string_hash;
?>
The encrypted key is saved in another file named hash.txt in same folder.
echo $user_passkey
and echo $file_password
both are showing exact hash key(same 'password' given for both user input and previously encrypted key file hash.txt) but not getting compared in the if statement.
Why does it not work as intendet?
';` is wrong because you added `
` to the hash. And then I'll bet your file contains linebreak(s). Use `trim(fread($handle, filesize($filename)))` to remove them. P.S. Consider using `json_encode` and `json_decode` to add multiple username/password logins to your file. – mgutt Sep 04 '19 at 10:32