我有一个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
5 d41402abc4b2a76b9719d911017c592 7 d793037a0760186574b0282f2f435e7我想比较do值用户输入并将它们转换为md5,然后与TXT文件中的一个进行比较。我不知道为什么我的代码没有输出正确的答案,即使我输入了正确的值(user:hello password:world)。对不起,我的英语不好
当你使用file()时,文件中的每一行都成为数组中的一项。
但是,该数组中的每个字符串都包含换行符('n)。所以你需要确保你做的是…trim($read_file[0],"'r'n");
…。