0

I'm working on a log in form that users a text file (users.txt) to validate username/password against. I cannot use MYSQL for this.

The text file records are in this format:

user one:[email protected]:user1:password1

user two:[email protected]:user2:password2

If it validate just the USERNAME only, then it successfully checks the user: if ($currUser == $userUsername) {$valid = true; break;}BUT if I then try to validate both username and password I get the wrong result.($currUser == $userUsername && $currPass == $userPass) {$valid = true; break;} Results in "Invalid username or password"

I can't figure out what I'm doing wrong? When I echo the username and passwords they are a match!!!

SCRIPT:

if(isset($_POST['submit'])){
   $form_is_submitted = true;

   //FORM PROCESSING
   if(isset($_POST['userName']) && isset($_POST['password'])) {

      $currUser = $_POST['userName'];
      $currPass = $_POST['password'];
      $valid = false;//flag
      while (!feof($fileHandle)) {
          $userRow = fgets($fileHandle);
          $userDetails = explode(':', $userRow);
          if (!isset($userDetails[2])) {
              $userDetails[2] = null;
          }
          if (!isset($userDetails[3])) {

              $userDetails[3] = null;
          }
          $userUsername = $userDetails[2];
          $userPass = $userDetails[3];
          if ($currUser == $userUsername /*&& $currPass == $userPass*/) {
              $valid = true;
              //break;
          }
      }    
      if ($valid) {
         echo "<br> $userUsername logged in sucessfully<br>";
      } else {
         echo "<br>Invalid user name or password<br>";
         //FOR DEGUGGING ONLY!
         echo $currUser . $userUsername;
         echo $currPass . $userPass;
         echo $_POST['password'];
         echo $_POST['userName'];

      }
  } else {    
     $errors_detected = true;
     $errors['not set'] = "Please enter username and password";
  }

}
2
  • 3
    Maybe you got some special character (line feed or whatever) in the password string. Have you tried using trim() on $userPass? Commented Jul 27, 2015 at 15:15
  • @Typoheads great shout! 8 hours messing around with this and all that was needed was trim() Commented Jul 27, 2015 at 15:26

1 Answer 1

2

the fgets() function returns a line INCLUDING the linefeed \n (and the carriage return \r if its there). that means you have to remove those.

just change this:

$userPass = $userDetails[3];

to this:

$userPass = trim($userDetails[3]);

and it should work

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.