0

After several attempts, I have no idea why the log in is not working. I'm trying to log the user in (user password and email stored on a .txt file, each line has count,useremail,password) and then redirect them to index2.php for only users to access. I'm not sure what I'm doing wrong.

log in form located in navbar in index.php

<form class="form" role="form" method="POST" action="login.php" accept-charset="UTF-8" id="login-nav">
    <div class="form-group">
        <label class="sr-only" for="email">Email address</label>
        <input type="email" class="form-control" id="email" placeholder="Email address" required>
    </div>
    <div class="form-group">
        <label class="sr-only" for="password">Password</label>
        <input type="password" class="form-control" id="password" placeholder="Password" required>
    </div>
    <div class="form-group">
    <button type="submit" class="btn btn-primary btn-block" id="submit">Sign in</button>
    </div>
</form>

login.php code

<?php session_start();?>
<?php
$email=$_POST['$email'];
$pass=$_POST['$password'];
echo $email;
$fileName = "Data/users.txt";
$target = fopen($fileName, "r");

//find user
$users=fgetcsv($target,200,",");
while(!feof($target)){
    if($email==$users[1]&&$pass==$users[2]){
        $_SESSION['email']=$users[1];
        $_SESSION['id']=$users[0];
        //if found redirect
        $redirect= "index2.php";
        header("Location:$redirect");
        exit();
    }
    else{
    unset($_SESSION['email']);
    unset($_SESSION['id']);
    echo "<script>";
    echo "alert(Invalid login.);";
    echo "location.href='index.php?error=login';";
    echo "</script>";
    }
}
fclose($target);
?>
bviale
  • 5,245
  • 3
  • 28
  • 48

1 Answers1

0

You can't echo $email; and then do a header() redirect. You will have to enable output buffering:

ob_start();
echo $email;
// other code
header("Location:$redirect");
ob_end_flush();

And enable error reporting to see what is going wrong.

Community
  • 1
  • 1
Tom Udding
  • 2,264
  • 3
  • 20
  • 30