0

I am writing php to verify login credentials where the credentials (user,pass) is stored in a txt file: The text file has an empty line at the end.

users.txt

BobRock123,password123
DouglasFerm553,secret642
SallyHomes,Yse3qs
Vinny893,street3r2

php

$userFile = fopen("users.txt", "r") or exit("Unable to open file!");
while (!feof($userFile) && fgets($userFile) != "")
{
     $line = explode(",", fgets($userFile));
     if ($username == $line[0] && $password == $line[1])
     {
          header("Location:  nextpage.php");
          exit;
     }
}

I am not able to redirect to nextpage.php even trying with matching username and passwords (all matching username and passwords fail). If I replace the if line with if (true), I will be redirected, confirming that I am able to open my file.

DanCode
  • 525
  • 3
  • 7
  • 25
  • `fgets()` will normally have the *new line* from the file on the end of the string, so it will be trying to match the password with the password plus some whitespace. Using `trim(fgets($userFile))` should solve the problem. – Nigel Ren May 16 '20 at 07:45
  • @NigelRen, that didn't work. – DanCode May 16 '20 at 07:49
  • Have you corrected the various other bugs in your code - missing comma in `explode("," fgets($userFile))`, missing `;` on the `header` line. You also use `fgets` in the loop and the explode - so reading two lines per loop. Change loop to `while (!feof($userFile) && ($line = fgets($userFile)) != "")` and explode to `explode(",", trim($line))` – Nigel Ren May 16 '20 at 08:00

0 Answers0