解决会话中的问题?

solving problems in session?

我创建了两个页面,一个是 index.php 和 admin.php。在第一页或 index.php 中,我创建了一个登录表单,以便我可以通过登录或类似 enter image description here

访问 admin.php 页面

现在登录后我会转到 admin.php 页面。这是我现在想问的问题,每当我单击 chrome 中的后退按钮或下一步按钮时。我要返回 admin.php 页面。我已经尝试了 session_start() 和 if(!isset($password) || !isset($user)){}。但是由于显而易见的原因,这段代码不起作用。那么有人可以帮我解决这个问题吗?

示例代码在这里index.php

<?php 
session_start();



?>
<!DOCTYPE html>
<html lang="en">
<head>

    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="style.css">
    
    <title>Admin</title>
    </head>
<body>
    





<a href="reset.php">Reset</a>

    
    
    


    <div class="image">

        <img src="img/adi.png" alt="image">                                         <!-- image -->

    </div>



    
    
    
    
    
    
    <form action="inc/login.php" method="POST"><br>
        
        <p class="title">Log In</p>
    
        <label for="Username">User :</label>
            <input type="text" name="username"             id="user"> <br>            <!--username  -->
        
            
        <label for="Password">Password :</label>
            <input type="password" name="password"         id="password"    ><br><br>        <!-- password  --> 


        <label for="showpassword" class="showpassword">Show Password</label>   
            
            
            <input type="checkbox" name="checkbox"   id="chk" ><br><br>               <!-- checkbox -->
        
        
        <input type="submit" name="submit" value="Log in" >                       <!--  enter -->

        
       
    
    </form>
    
    <?php
    
    if(!isset($_GET['Login'])){
            exit();
        }else{

            $check=$_GET['Login'];
        
        
            if($check=="userEmpty"){
                
                echo "<p class='class_login'>user is empty</p> ";
            
            }elseif($check=="passwordEmpty"){
                
                echo "<p class='class_login'>password is empty</p> ";
            
            }elseif($check=="wrongUser"){
                
                echo "<p class='class_login'>user is wrong</p> ";
            
            }elseif($check=="Password"){
                
                echo "<p class='class_login'>password is wrong</p> ";
            
            };
    
    
        } ;
    ?>

 <script src="js/main.js"></script>

</body>
</html> 

admin.php 的代码是这个:

<?php
session_start();

if(!isset($username) || !isset($password)){

    header("location:index.php?data=closed");
    exit();
}


?>

<!DOCTYPE html>
<html lang="en">
<head>
    
    <script>
        window.history.forward();
    </script>

    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="style.css">
    <title>Document</title>
</head>
<body>

    <div class="top">
        <a href="inc/logout.php">Log Out</a>
    </div>
    
</body>
</html>

除了使用 session_start 你需要在会​​话中存储一些东西,使用这样的东西:

$_SESSION['userId'] = <value>;

必须在您的 login.php 中设置并由您的 admin.php 检查用户是否有权访问安全页面。 IE。您需要检查:

if (isset($_SESSION['userId']) && $_SESSION['userId'] == <value>) {
  // access granted
}