0
我有一個簡單的登錄和註銷系統,在WampServer中使用PHP。登錄表單位於index.php,提交後會導致login.php。並且login.php有註銷的鏈接(logout.php)。用PHP登錄並註銷後防止用戶返回
的index.php
<html>
<head>
</head>
<body>
<form action="login.php" method="POST">
Username:<input name="username" type="text">
<br>
Password:<input type="password" name="password">
<br>
<input type="submit" name="submit" value="login">
</form>
</body>
</html>
的login.php
<html>
<head>
</head>
<body>
<?php
define('DB_NAME','db_name');
define('DB_USER','root');
define('DB_PASSWORD','');
define('DB_HOST','localhost');
$link=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
if (!$link)
{
die('Failed to connect to MySQL: ' . mysql_error());
}
$query = "CREATE DATABASE IF NOT EXISTS " . DB_NAME;
if(!mysql_query($query)){
die('Error in creating database : <br> ' . mysql_error());
}
$db_selected = mysql_select_db (DB_NAME, $link);
if(!$db_selected) {
die(' Can\'t use ' . DB_NAME . ' : ' . mysql_error());
}
session_start();
if (isset($_POST['submit'])) {
$username = $_POST['username'];
$password = $_POST['password'];
if ($username && $password){
$query = mysql_query("SELECT * FROM login WHERE username='$username'");
$numrows = mysql_num_rows($query);
if($numrows!==0){
while($row = mysql_fetch_assoc($query)){
$dbusername = $row['username'];
$dbpassword = $row['password'];
}
if ($username==$dbusername && $password==$dbpassword) {
echo "You are logged in !";
@$_SESSION['username'] = $username;
}
else{
die("password incorrect");
}
}
else{
die("User does not exist");
}
}
else {
die("Please enter correct username and password");
}
}
?>
<br>
<a href="logout.php">Logout</a>
</body>
</html>
logout.php
<?php
session_start();
unset($_SESSION["username"]);
header("Location: index.html");
?>
這是工作。
現在,我想阻止以下內容。
- 註銷
- 已經登錄
- 去的login.php並通過輸入這些頁面的URL的index.html後回去的index.html後回去的login.php。
我曾嘗試在login.php中添加以下內容以清除緩存,但尚未解決。
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
而且
header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header ("Pragma: no-cache");
事實上,如果我不寫任何高於2個代碼片段,瀏覽器會要求重新提交表單。但是如果我寫它,它會直接顯示頁面而不會阻止任何內容。
我在這個網站上看過this question,但無法理解如何應用它。任何人都可以請解釋如何清除緩存或在PHP中使用任何其他方法來做到這一點?
我插入上面的代碼在index.html的的端部。它不工作。 –
不是在最後,而是在html標籤之前的頂部 – MD9
在頂部。還是行不通。 –