简体   繁体   中英

Can't login on form?

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>

You are sending a username and password

  <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>

But you are checking for an email

$_POST['email']

Try changing

if( $_POST['email'] == $email && $_POST['userpass'] == $userpass ) {

to

if( $_POST['username'] == $email && $_POST['password'] == $userpass ) {

what you have set in the name of the input will be the name of the POST variable.

 name="username" will be $_POST['username']

It seems like your form does not send the email and userpass fields to your Login.php. Or at least not with those names.

Make sure you correctly specify the names of your <input> tags like so:

<form method="post" action="Login.php">
   <input type="text" name="email" />
   <input type="password" name="userpass" />

   <input type="submit" name="submitBtn" value="Submit" />
</form>

Furthermore, you should check, if the email and userpass keys are set before evaluating them (like you did with the submitBtn ).

if (isset($_POST['email']) && isset($_POST['userpass'])) {
    // ...
}

Oh, and one last thing. Please don't save passwords in an unencrypted text file. Use something like bcrypt or at least MD5 or SHA1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM