-2

I have tried and tried to get this to work. Essentially I want to read a line of a text file and compare it to the user input. If they are the same then it will continue the log in process.

For now I am just using an echo to see if it actually finds something. it seems that the if statement doesn't see the two inputs as a match even when they are.

$myFile = fopen("users.txt", "r");
$username = $_POST['username'];

while(!feof($myFile))
{
    $userN = fgets($myFile);
    //compares entered user to text file users
    if (($username === $userN)){
        echo 'found';
    }
}

The only time that it ever finds a match is if the input is left blank as it will be matched with the final line of the file.

Callum
  • 1
  • 1
  • Depending on how many users you have you could use `file` and `in_array`. What does `$userN` come out as with this code? I usually use `fread` with the `fopen`. – user3783243 Jan 05 '19 at 16:14
  • When reading the file, you must use trim, rtrim to remove the eol terminator(s) before trying to use the line from the file in your if() block... $userN = trim ( fgets ( $myFile ) ); – Stephanie Temple Jan 05 '19 at 16:22
  • 2
    Side note: Any special reason why you're using text files rather than a database for this? It'd be a lot simpler/easier; believe me. – Funk Forty Niner Jan 05 '19 at 16:32
  • As a one liner `if(in_array($_POST['username'], file("users.txt"))) {` – user3783243 Jan 05 '19 at 16:35

3 Answers3

1

Try this code.

<?php
 $myFile = fopen("users.txt", "r");
 $username = $_POST['username'];

 while(!feof($myFile))
{
 $userN = fgets($myFile);

 //$userN = rtrim(fgets($myFile, "\r\n")); //More cleaner way suggested in comment

 //compares entered user to text file users
  if ($username == trim($userN)){
    echo 'found';
  } else {
    echo "non found";
}
}

I guess $userN is taking some white spaces or other predefined characters before or after string, so use trim() to remove it.

Prashant Deshmukh.....
  • 2,244
  • 1
  • 9
  • 11
  • `fgets` will return the newline character as well, so comparison probably looks something like `if ("Callum" === "Callum\n")`. Cleaner method would be to `rtrim(fgets($myFile, "\r\n"))` https://stackoverflow.com/questions/7478250/how-do-i-eliminate-line-break-from-fgets-function-in-php – Danny Battison Jan 05 '19 at 16:24
  • If the user isn't in the file, you get something like `non foundnon foundnon foundnon found` – Nigel Ren Jan 05 '19 at 16:37
1

Although it's not the best way to check users exist, you can load all the users using file() (without the new line and removing any blank entries), which will produce an array of the users. Then just use in_array() to see if the user is in the list...

$username = $_POST['username'];
$userList = file("users.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if ( in_array($username, $userList))    {
    echo "Found";
}
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
0

how are you store user information? are you using commas or something else to separate users and can't you use JSON instead of this in that way it's much easier to store, retrieve and check. if you still need to use i wold like to suggest this method first get all and store in string then convert it to array using PHP function explode and then check the array (assuming text file only contain user name and user separate by , )

myuser.txt

jhone,jonney,mark kevin

read.php

$file = file_get_contents('./myuser.txt', FILE_USE_INCLUDE_PATH);
$arry = explode(",",$file);
$count = 0;
while($count<sizeof($arry)){
  if($arry[$count]== $userInput){
      echo 'found';
  }else{
       echo "non found";
  }

  $count++;

}
Thalinda Bandara
  • 1,039
  • 1
  • 11
  • 27