注销后,按浏览器的“后退”按钮,它将重定向到上一页

时间:2019-06-09 03:40:38

标签: php

注销后,它会转到登录页面,但是当我从浏览器中按“后退”按钮时,它会转到未正确登录的上一页。但是我尝试在注销后尝试此操作,直到登录用户无法访问页面为止。

我希望注销后,当用户按下“后退”按钮时,它会一直进入登录页面。但是在我的代码中,注销后按返回按钮,它会转到上一页,并且用户可以在注销后访问页面。

我是PHP的初学者。请帮帮我。

login.php

<?php
session_destroy();
session_start();
$message="";
if(count($_POST)>0) {
 $con = mysqli_connect('localhost','username','password','db name') or die('Unable To connect');
$result = mysqli_query($con,"SELECT * FROM login WHERE username='" . $_POST["username"] . "' and password = '". $_POST["password"]."'");
$row  = mysqli_fetch_array($result);
if(is_array($row)) {
$_SESSION["id"] = $row[id];
$_SESSION["username"] = $row[username];
{
if (isset($_POST["username"]) AND $_POST["username"] == 'moyna') {
header("Location:welcomemoyna.html");
}
elseif(isset($_POST["username"]) AND $_POST["username"] == 'ramtarak') {
header("Location:welcomeramtarak.html");
}
elseif(isset($_POST["username"]) AND $_POST["username"] == 'tamluk') {
header("Location:welcometamluk.html");
}
elseif(isset($_POST["username"]) AND $_POST["username"] == 'bagnan') {
header("Location:welcomebagnan.html");
}
elseif(isset($_POST["username"]) AND $_POST["username"] == 'admin') {
header("Location:welcomeadmin.html");
}
elseif(isset($_POST["username"]) AND $_POST["username"] == 'quickd') {
header("Location:welcomequickd.html");
}
}
}
else {
$message = "Invalid Username or Password!";
}
// }
}
?>
<?php
// remove all session variables
session_unset(); 

// destroy the session 
session_destroy();

echo "All session variables are now removed, and the session is destroyed." 
?>
<html>
<head>
        <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
    <meta name="description" content="" />
    <meta name="author" content="" />
    <title>Quick Group</title>
     BOOTSTRAP CORE STYLE CSS 
    <link href="assets/css/bootstrap.css" rel="stylesheet" />
     FONT AWESOME CSS 
<link href="assets/css/font-awesome.min.css" rel="stylesheet" />
      FLEXSLIDER CSS 
<link href="assets/css/flexslider.css" rel="stylesheet" />
     CUSTOM STYLE CSS 
    <link href="assets/css/style.css" rel="stylesheet" />    
   Google   Fonts 
    <link href='http://fonts.googleapis.com/css?family=Open+Sans:400,700,300' rel='stylesheet' type='text/css' />

<title>User Login</title>
</head>
<body>
    <div class="navbar navbar-inverse navbar-fixed-top " id="menu">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#">QUICK GROUP</a>
            </div>
            <div class="navbar-collapse collapse move-me">
                <ul class="nav navbar-nav navbar-right">
                    <li ><a href="index.html">HOME</a></li>
                     <li><a href="#">FEATURES</a></li>
                     <li><a href="#">COURSES</a></li>
                     <li><a href="#">CONTACT</a></li>
                     <li><a href="login.php">LOGIN</a></li>
                </ul>
            </div>

        </div>
    </div>
    <br>
    <br>
    <br>
    <br>
<form name="frmUser" method="post" action="" align="center">
<div class="message"><?php if($message!="") { echo $message; } ?></div>
<h3 align="center">Enter Login Details</h3>
 Username:<br>
 <input type="text" name="username">
 <br>
 Password:<br>
<input type="password" name="password">
<br><br>
<input type="submit" name="submit" value="Submit">
<input type="reset">
</form>
<center>
<a href="passwordchange.php">Change password</a>
<!--<a href="newexpenseview.php">Change password</a>-->
</center>
</body>
</html>

logout.php

<?php
session_start();
unset($_SESSION["id"]);
unset($_SESSION["username"]);
header("Location:login.php");
?>

1 个答案:

答案 0 :(得分:0)

在php中实现此功能需要您:

  

在每个页面上检查用户是否已登录,使用变量表示登录状态(如果未登录)   那么用户应该被重定向回登录页面。

<?php 
  if(!isset($_SESSION['logged_in'])) : 
  header("Location: loginPage.php");  ?>

然后,在注销时,您需要取消设置已登录的状态变量并破坏会话,因此,如果用户现在按回,则将找不到已登录的状态变量,并且不会加载页面。 / p>

<?php
  unset($_SESSION['logged_in']);  
  session_destroy();  ?>

-谢谢提问。