【发布时间】:2019-09-04 09:45:34
【问题描述】:
为什么在比较两个相等的 md5 键后显示incorrent password?
<?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>
另一个 md5 创建的文件是:
<?php
$string = 'password';
$string_hash = md5($string);
echo $string_hash;
?>
加密的密钥保存在同一文件夹中另一个名为 hash.txt 的文件中。
echo $user_passkey 和 echo $file_password 都显示了准确的哈希密钥(为用户输入和先前加密的密钥文件 hash.txt 提供了相同的“密码”),但未在 if 语句中进行比较。
为什么它不起作用?
【问题讨论】:
-
当 php has one built in 时,你为什么还要滚动自己的密码哈希?
-
$user_passkey = md5($user_password).'<br>';是错误的,因为您将<br>添加到哈希中。然后我敢打赌你的文件包含换行符。使用trim(fread($handle, filesize($filename)))删除它们。附言考虑使用json_encode和json_decode将多个用户名/密码登录添加到您的文件中。