0

我有一个简单的登录表单,如果出现错误(例如密码错误),我需要它能够记住输入的用户名。我会去做这个 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>');

}

?>

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

4

2 回答 2

1

您可以使用 $_SESSION 或 $_COOKIE。

对于 $_COOKIE,只需使用setcookie(...) (for localhost, on $domain, use false)

对于 $_SESSION,只需像任何其他数组一样设置它

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

于 2012-05-31T03:47:08.140 回答
0

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

//Else 回显登录失败。die('登录失败,点击这里重试');

在表单中 if (isset($_GET['username']) 将其设置为登录用户名字段,否则将其保留为空

于 2012-05-31T04:50:25.933 回答