Keep username in username field when incorrect password
How do many of the websites that have logins keep the username in the username field when the password is entered incorrectly. The form's action (it is on index.php) is another php file, which if the password is incorrect uses Header() to go back to the index and passes the error to it using GET. I could use GET to pass the username back to the form but this seems messy, and I have noticed that websites like SMF forums have the username stay in the username field 开发者_如何学JAVAwithout a messy URL.
I normally find it easier to process form data in the same script where the form is displayed and redirect somewhere else only when I've gathered correct input.
Simples!
<form action="loginpage.php" method="post">
<fieldset>
...
<input type="text" name="username" value="<?php if (isset($_POST['username'])) echo $_POST['username']; ?>" />
...
</fieldset>
</form>
Rest of the form ommitted to emphasize solution.
You can use sessions.
For example, when a post is incorrect (this would go in the file processing your post request):
$_SESSION['username'] = $_POST['username'];
Then on your login page:
<input type="text" name="username" value="<?php echo @$_SESSION['username']; ?>" />
Make sure you start the session though: http://php.net/session_start
Through GET method
header("Location: loginpage.php?username=".$_REQUEST['username']);
and in form page u can access it like:
<input type="text" name="username" value=<?php echo $_GET['username']?>
精彩评论