Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 218
  • Last Modified:

form?

Say i have a form, which request the user to input username and password. it also have a submit button.
its "action" is set to "submit.php"
when i click the submit button, i would like to verify the username and password.
if it is valid, it does something. if it is invalid, i want it to go back to the previous page, BUT this time, the username and password that the user input previousely is there in the username and password field.

i tried something like this:
<form action="submit.php" method="post">
    username : <input type="text" name="username">
    password : <input type="password" name="password">
   <input type="submit">
</form>

but whenever i checked in submit.php that the username and password are invalid, and when i hit the back button of my browser, all the fields are empty.

how do i resolve this?

thx
0
sutejok
Asked:
sutejok
2 Solutions
 
matt_mcswainCommented:
You could use session variables, so on submit.php

session_start();
if(!validuser) {
  $_SESSION['username'] = $_POST['username']
}

Then on your form page
<?php
session_start();
?>
<form action="submit.php" method="post">
    username : <input type="text" name="username"
            value="<? echo isset($_SESSION['username']) ? $_SESSION['username'] : '';?>"/>
    password : <input type="password" name="password">
   <input type="submit">
</form>

But the general solution is to do your user/pass check on the form page and on success redirect, otherwise keep them and the form page and use the POST data to refill your form fields.
 
 
0
 
venkateshwarrCommented:
In your submit.php page, instead of going back present the login forma again with previously typed values

<form action="submit.php" method="post">
    username : <input type="text" name="username" value="<?php echo $_REQUEST['username'] ?>">
    password : <input type="password" name="password" value="<?php echo $_REQUEST['password'] ?>">
   <input type="submit">
</form>
0
 
minichickenCommented:
Hi

You can use sessions to temporily store the username and password.
========================================================
First of all to use sessions, have this at the right at the top of the pages.

<?php
session_start();
header("Cache-control: private"); // IE 6 Fix.
?>

========================================================
LOGIN.PHP
<form action="submit.php" method="post">
    username : <input type="text" name="username" value ="<?php echo  $_SESSION['login_username'];?>">
    password : <input type="password" name="password" value ="<?php echo  $_SESSION['login_password'];?>">
   <input type="submit">
</form>
========================================================
SUBMIT.PHP

$_SESSION['login_username'] = $_POST['username'];
$_SESSION['login_password'] = $_POST['password'];

if ($_SESSION['login_username']  == "MY_USERNAME" && $_SESSION['login_password'] == "MY_PASSWORD")
{
      echo "VALID";
}
else
{
      header ("Location: login.php");
      exit;
}
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now