【问题标题】:compare user and password with md5 in txt?将用户名和密码与 txt 中的 md5 进行比较?
【发布时间】:2013-04-11 22:31:50
【问题描述】:

我有一个 php 文件:

<?php  
 if (isset($_POST['submit'])) { 
    $file_name = 'hash.txt'; 
    $user = md5($_POST['user']); 
    $password = md5($_POST['password']); 
    $handle = fopen('hash.txt',"r"); 
    $read_file = file($file_name);


    if ($user == $read_file[0] and $password == $read_file[1]) { 
        echo 'correct !'; 
    } else { 
        echo 'incorrect !'; 
    } 
} else { 
    echo 'please input something'; 
}
?> 
<body> 
<form action="file_handle3.php" method="post"> 
User<input type="text" name="user"/><br/> 
Password <input type="password" name="password"/> 
<input type="submit" name="submit" value="submit"/> 
    </body>

文件 txt : hash.txt 第一行是 hello 第二行是 world

5d41402abc4b2a76b9719d911017c592

7d793037a0760186574b0282f2f435e7

我想比较用户输入的值并将它们转换为 md5,然后与 txt 文件中的比较。我不知道为什么我的代码即使我输入准确的值也不会输出正确的答案(用户:你好密码:世界)。对不起我的英语不好

【问题讨论】:

  • 如果您的 .txt 文件中有哈希,那么您应该将您的 md5 哈希密码与文件的哈希值进行比较,即 md5($password) == $read_file[1]。也许如果您将它作为纯文本接收一次,然后比较哈希值就可以了。您也可以尝试检查 md5($_POST['password']) 输出是否与您的 .txt

标签: php


【解决方案1】:

当您使用 file() 时,文件中的每一行都将成为数组中的一项。

但是,该数组中的每个字符串都包含换行符 (\n)。因此,您需要确保执行类似...

trim($read_file[0],"\r\n"); 

...首先。

【讨论】:

  • 在调用 file() 函数时,不要修剪结果,而是使用 FILE_IGNORE_NEW_LINES 标志
  • $read_file = file($file_name, FILE_IGNORE_NEW_LINES);也可用。
  • 我希望我在 5 年前就意识到这一点。一直在修剪';)
猜你喜欢
  • 1970-01-01
  • 2021-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多