the code is running to match username with password and if both correct redirecting to another page but that and the else statements both run
Code:
if (isset($_POST['Login'])) {
    $IncorrectDetails = 0;
    $Username=$_REQUEST['Username'];
    $Password=$_REQUEST['Password'];
    echo "<p> $Username  $Password</p>";
    $myfile = fopen("bin\Account Details.txt", "r") or die("Unable to open file!");
    //reads raw file
    $string = fread($myfile,filesize("bin\Account Details.txt"));
    //turns the string to array
    $a = explode(',', $string);
    foreach ($a as $result) {
        $b = explode('. ', $result);
        $AccountDetails[trim($b[0])] = trim($b[1]);
    }
    //closes file
    fclose($myfile);
    print_r($AccountDetails);
    foreach ($AccountDetails as $StoredUsername => $StoredPassword) {
        if ($StoredUsername == $Username){
            if ($StoredPassword == $Password) {
                header('Location: Main.php');
            }
            else {
                $IncorrectDetails = 1;
            }
        }
        else {
            $IncorrectDetails = 1;
        }
    }
    if ($IncorrectDetails == 1){
        echo "<script type='text/javascript'>alert('Incorrect login details');</script>";
    }
}
its expected to come up with a popup when incorrect and redirect when correct
 
     
     
    