PHP 错误表单 - 将表单的内容保留在重定向上


PHP Error Form - Leave Contents of Form on Redirect

我有一个简单的登录表单,如果发生错误(例如密码错误),我需要它能够记住输入的用户名。 我会继续做这个PHP或Javascript,因为我不允许使用JQuery。

我当前的PHP - (不包括HTML表单)

<?php
//MySQl Connection
 mysql_connect("localhost", "root", "") or die(mysql_error()); 
 mysql_select_db("clubresults") or die(mysql_error()); 
//Initiates New Session - Cookie
session_start(); // Start a new session
// Get the data passed from the form
$username = $_POST['username'];
$password = md5($_POST['pass']);
// Do some basic sanitizing
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
//Performs SQL Query to retrieve Login Details from DB
$sql = "select * from admin_passwords where username = '$username' and password = '$password'";
$result = mysql_query($sql) or die ( mysql_error() );
//Assigns a Variable Count to 0
$count = 0;
//Exectues a loop to increment on Successful Login
while ($line = mysql_fetch_assoc($result)) {
     $count++;
}
//If count is equal to 1 Redirect user to the Members Page and Set Cookie
if ($count == 1) {
     $_SESSION['loggedIn'] = "true";
     header("Location: members.php"); // This is wherever you want to redirect the user to
} else {
//Else Echo that login was a failure.
die('Login Failed. <a href=login.php>Click Here to Try Again</a>');
}
?>

任何帮助将不胜感激。干杯

您可以使用

_SESSION 美元或 _COOKIE 美元。

只需 _COOKIE 美元,只需使用 setcookie(...)(对于 localhost,在 $domain 上,使用 false

只需 _SESSION 美元,只需将其设置为任何其他数组即可

要检查该值是否存在,请使用 isset(...)。

不要使用会话变量。您可以通过在登录失败时将用户名作为参数传递给登录表单来实现这一点。

否则回声登录失败。die('登录失败。单击此处重试");

在形式上如果(isset($_GET['用户名'])将其设置为登录用户名字段,否则将其留空