How to kill a session after logging out using php

前端 未结 2 1526
礼貌的吻别
礼貌的吻别 2021-01-29 02:34

I have seen the following question and tried to adapt part of the answer but to no luck: \'How can i disable the back browser button after user press logout and destroy session?

相关标签:
2条回答
  • 2021-01-29 02:42

    As for headers, here are the ones from a web server I regularly use after a search:

    Cache-Control: private, pre-check=0, post-check=0, max-age=0
    Expires: 0
    Pragma: no-cache
    

    Pressing the back button after clicking a link from the results page displays an error message in Firefox. The only notable difference I see is Expire: 0.

    0 讨论(0)
  • 2021-01-29 02:53

    login page:

    <?php 
    if (isset($_POST['uname'], $_POST['pwd'], $_POST['type'])) {
        $Username = $_POST['uname'];
        $Password = $_POST['pwd'];
        $User_Type=$_POST['type'];
        if (!(empty($Username) || empty($Password) || empty($User_Type))) 
        {
             $model = new UsersModel();
             $rowsCount = $model->checkUser($Username,$Password,$User_Type);
             if ($rowsCount!=0)
             {
                  $_SESSION['user'] = $Username;
                  header("Location:LoginViewController.php");
    
             } else {
                  echo 'Bad user';
             }
        } else {
             echo 'Please, fill all inputs';
        }
    } else {
        echo 'Bad form sent';
    }
    ?>
     <form name="f1" method="POST" action="" >
    // inputs
     </form>
    

    LoginViewController.php :

    <?php
    header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
    header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
    
    if (!isset($_SESSION['user'])) {
    header('Location: login.php');
    exit();
    }
    echo 'You have successfully logged as '.$_SESSION['user']
    ?>
    

    And add the headers to force the browser to revalidate the pages :

    logout.php :

    <?php 
    session_start();
    session_destroy();
    $_SESSION = array();
    header("location: login.php");
    ?>
    
    0 讨论(0)
提交回复
热议问题