I have the below code which gets the username submitted from the previous page, opens the corresponding text file, and then verifies whether the password is correct. If I enter a correct username and password, it says, "Invalid password". If I leave the password field blank then I get "Username and password correct", even though it's not. If I echo the password from the previous page and the password in the text file I can see they are both correct. So what's going on, why does the script say I entered the password wrong when it's right?
<?php
if (file_exists("users/$username.txt"))
{
$filename = "users/$username.txt";
$fp = fopen($filename, "r");
$file_contents = fread($fp, filesize($filename));
fclose($fp);
$line = explode("\n", $file_contents);
if ($password==$line[1])
{
echo "Username and password correct";
}
else {
echo "Invalid password";
}
}
else {
echo "Invalid username";
}
?>