0

as you can see by my code, if the variable $uname and $password are not equal to the values they should be in the text file im reading from, it should print and error. Unfortunately it does not. When the values are correct however it prints both the success message and the failure message on the same line instead of just the success message. I am sure I'm overlooking something very simple but still need to ask. The code is below. Thanks in advance!

<?php

if (isset($_POST['submit'])) {
    $uname = $_POST['uname'];
    $pass = $_POST['pass'];
    $password = sha1($pass);
    $filename = "pass.txt";
    $contents = file($filename, FILE_IGNORE_NEW_LINES );
    foreach ($contents as $value) {
        $details = explode(':', $value);
        if ($uname == $details[0]&& $password == $details[1]) {
          echo" Welcome, Access Has Been Granted!";
        }  else {
          echo "Please Check User Name and Or Password";
          exit;
        }

    }

}

?>
3
  • 2
    Is there more code in this file that could be doing the printing? I don't think if/else is broken. Commented Nov 16, 2020 at 18:59
  • You're performing this operation in a loop. How have you determined that the if block and the else block are not executing on separate iterations of that loop? Seems far more likely than if/else being fundamentally broken as you propose. Commented Nov 16, 2020 at 19:03
  • 1
    Use a break; to exit the foreach loop after find the user. Commented Nov 16, 2020 at 19:03

1 Answer 1

1

Your password check is inside a loop, and that loop will repeat for every line in pass.txt -- even if a line is totally empty. You should modify your code so that a $password_success variable defaults to FALSE and is only set to true if a valid, matching user/password line exists. Then, outside the loop, check $password_success exactly once.

<?php

if (isset($_POST['submit'])) {
    $uname = $_POST['uname'];
    $pass = $_POST['pass'];
    $password = sha1($pass);
    $filename = "pass.txt";
    $contents = file($filename, FILE_IGNORE_NEW_LINES );
    $password_success = FALSE;
    foreach ($contents as $value) {
        $details = explode(':', $value);
        if (sizeof($details) == 2) {
            if ($uname == $details[0]&& $password == $details[1]) {
                $password_success = TRUE;
                break; // this will stop our password file looping
            }
        } else {
            // skip invalid password file line

        }
    }

    if ($password_success) {
      echo" Welcome, Access Has Been Granted!";
      exit;
    }  else {
      echo "Please Check User Name and Or Password";
      exit;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

@JacobSBaker you're welcome. feel free to select my answer as the solution. i'm all about the points!

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.