我试图从文本文件中读取密码,并将其与用户输入的密码进行比较。但是,尽管在我看来这两个词完全一样,但我的脚本还说了别的:
$user = $_POST['user'];
$pass = $_POST['pass']; //HTML form element with type="password"
$line;
$retrievedPassword = "";
$f = fopen("data/abc.log", "r");
// Read line by line until end of file
while(!feof($f)) {
$line = fgets($f);
if (startsWith($line, $user)) {
break; //password found
}
}
fclose($f);
$var = explode(":", $line);
$retrievedPassword = $var[1];
echo $pass." ".$retrievedPassword; // example: password password
if (strcmp($pass, $var[1]) == 0) {
header('Location: user.php'); //never the case
}else {
//header('Location: index.php');
}
function startsWith($haystack, $needle)
{
return !strncmp($haystack, $needle, strlen($needle));
}
密码是加密的还是类似的,或者为什么这个代码不起作用?