我正在创建一个登录页面,以仅涵盖管理员的重要内容。这是我当前成功的代码。
<?php
$username = "adminuser";
$password = "adminpass";
$randomword = "helloworld";
if (isset($_COOKIE['MyLoginPage'])) {
if ($_COOKIE['MyLoginPage'] == md5($password.$randomword)) {
?>
<?php
$dbhost = 'localhost';
$dbuser = 'user';
$dbpass = 'pass';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
$dbname = 'jrmathem_service';
mysql_select_db($dbname);
$query = "SELECT * FROM interact";
$result = mysql_query($query)
or die(mysql_error());
print "
<h3>Interact Event Sign-Up Results</h3>
<table border=\"5\" cellpadding=\"5\" cellspacing=\"0\" style=\"border-collapse: collapse\" bordercolor=\"#808080\" width=\"100%\" id=\"AutoNumber2\" bgcolor=\"#C0C0C0\"><tr>
<td width=100>Name</td>
<td width=100>Grade</td>
<td width=100>Contact</td>
<td width=100>A</td>
<td width=100>B</td>
<td width=100>C</td>
<td width=100>D</td>
<td width=100>E</td>
</tr>";
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
print "<tr>";
print "<td>" . $row['name'] . "</td>";
print "<td>" . $row['grade'] . "</td>";
print "<td>" . $row['contact'] . "</td>";
print "<td>" . $row['A'] . "</td>";
print "<td>" . $row['B'] . "</td>";
print "<td>" . $row['C'] . "</td>";
print "<td>" . $row['D'] . "</td>";
print "<td>" . $row['E'] . "</td>";
print "</tr>";
}
print "</table>";
?>
<?php
exit;
} else {
echo "<p>Bad cookie. Clear please clear them out and try to login again.</p>";
exit;
}
}
if (isset($_GET['p']) && $_GET['p'] == "login") {
if ($_POST['name'] != $username) {
echo "<p>Sorry, that username does not match. Use your browser back button to go back and try again.</p>";
exit;
} else if ($_POST['pass'] != $password) {
echo "<p>Sorry, that password does not match. Use your browser back button to go back and try again.</p>";
exit;
} else if ($_POST['name'] == $username && $_POST['pass'] == $password) {
setcookie('MyLoginPage', md5($_POST['pass'].$randomword));
header("Location: $_SERVER[PHP_SELF]");
} else {
echo "<p>Sorry, you could not be logged in at this time. Refresh the page and try again.</p>";
}
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>?p=login" method="post"><fieldset>
<label><input type="text" name="name" id="name" /> Name</label><br />
<label><input type="password" name="pass" id="pass" /> Password</label><br />
<input type="submit" id="submit" value="Login" />
</fieldset></form>
此代码存在的问题是,登录并退出窗口后,无论是否为登录页面打开完全相同的链接,它都将永远在计算机上登录。所以,我的问题是
我可以在代码中特别添加什么以及在哪里添加代码,以便当您退出窗口或选项卡时,它会自动将您注销出页面,以便您再次登录时可以再次登录。 >
谢谢,我希望得到一些具体的反馈。
我正在创建一个登录页面,以仅涵盖管理员的重要内容。这是我当前成功的代码。
[首先让我解释一下您始终保持登录状态的原因。这是因为您正在使用Cookies。如果登录成功,则使用以下代码行设置Cookie:
即使不进行浏览,我如何使网站保持登录状态。因为我离开网站时经常遇到问题,几个小时后我的网站自动注销,所以我不想