douluan5444 2013-06-06 11:40
浏览 188
已采纳

如何在用户按下注销并销毁会话后禁用后退浏览器按钮?

I am having trouble with session_destroy().
When the User press Log out it have to destroy the session. I wrote the following code:

Logout.php

<?php
    session_start();
    session_destroy();
    header("location: LoginViewController.php");
?>

After pressing log out, when I press the browser back button it is showing my previous Logined user page and session username in Login.php page

Login.php

<?php
    session_start();
    $_SESSION['user']=  $_GET['username'];
    echo '"<div style="background:white; text-align:right"> Login as:'.$_SESSION['user'].'</div>"';
    echo '<a href="Logout.php" style="text-align:right">Logout</a>';

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");

    $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){
            header("location:login.php?username=".$_POST['uname']."");  
        } else {
            echo '<script type="text/javascript">alert("Enter username and password correctly");
            window.location.href="LoginViewController.php";</script>';
        }
    }

I don't know why it is working like that.
Please help me to find out where i commit mistake.

I want to disable that browser back button after logout.

  • 写回答

7条回答 默认 最新

  • dpb_4431 2013-06-06 12:30
    关注

    login.php 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");
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀