I'm trying to login on a site i made i have a registration form thats save data in a text file, then i have PHP code on my login.php page to open the file explode each line and then check the user entered a matching email and password when logging in. My problem Is I end up with a notice saying "undefined index: email" then it says username or password incorrect, even though they are correct. Do I need to declare the variable or use isset somehow to solve the problem or is there something wrong with my code?
Login.php:
<?php
if (isset($_POST['submitBtn'])){
$file = explode( "PHP_EOL", file_get_contents( "users.txt" ));
foreach( $file as $line ) {
list( $fname, $lname, $email, $userpass) = explode( "||", $line );
if( $_POST['email'] == $email && $_POST['userpass'] == $userpass ) {
// User authenticated correctly
echo "You've successfully logged in!";
echo "<br>";
echo '<a href="home.php">Member\'s Only Site!</a>';
} else {
// User did not authenticate
echo "Invalid Username or Password";
}
}
}
?>
Regform.php
else {
$userpass = md5($pass1);
$fp = fopen("Text_Database\users.txt", "a");
$savestring = $fname . "||" . $lname . "||" . $email . "||" . $userpass . "||" .PHP_EOL;
fwrite($fp, $savestring);
fclose($fp);
echo 'You have successfully registered!';
}
}
?>
Login form:
<form action="" method="post" name="loginform">
<table width="100%">
<tr><td>Username:</td><td> <input class="text" name="username" type="text" /></td></tr>
<tr><td>Password:</td><td> <input class="text" name="password" type="password" /></td></tr>
<tr><td colspan="2" align="center"><input class="text" type="submit" name="submitBtn" value="Login" /></td></tr>
</table>
</form>