Store username in username field if password is incorrect

How many of the websites that have logins store the username in the username field when the password is entered incorrectly. The form action (it's on index.php) is another php file that, if the password is wrong, uses Header () to return to the index and passes the error to it with a GET. I could use GET to pass the username back to the form, but that seems like a mess and I noticed that sites like SMF forums have a username in the username field without a messy url.

+2


source to share


4 answers


You can use sessions.

For example, when the message is wrong (this would be in the file handling your post request):

$_SESSION['username'] = $_POST['username'];



Then on the login page:

<input type="text" name="username" value="<?php echo @$_SESSION['username']; ?>" />

Make sure you start a session: http://php.net/session_start

+1


source


I usually find it easier to process form data in the same script where the form is displayed and redirected somewhere else, only when I have collected the correct input.



+3


source


Simples!

<form action="loginpage.php" method="post">
  <fieldset>
    ...
    <input type="text" name="username" value="<?php if (isset($_POST['username'])) echo $_POST['username']; ?>" />
    ...
  </fieldset>
</form>

      

The rest of the form is omitted to emphasize the solution.

+2


source


GET method

header("Location: loginpage.php?username=".$_REQUEST['username']);

      

and on the u form page you can access it like:

<input type="text" name="username" value=<?php echo $_GET['username']?>

      

0


source







All Articles