为什么每次运行登录序列后我的php $ _POST和$ _SESSION都无法清除?

问题描述

仅供参考:这是家庭作业,因此我无法发布所有代码

我在Ubuntu 18.04上以调试模式使用PHPStorm,只是为了确保已添加

$_POST = array();
$_SESSION = array();

清除登录成功后在登陆页面末尾view_mainpage.PHP以及在调用controller.PHP之前exit()中的那些数组。

我在controller.PHP中有一个switch语句,用于加入或登录登录的一半看起来像这样:

switch ($command) {  // When a command is sent from the client
        case 'SignIn':  // With username and password
            if (user_exists($username)== false) {//user does not exist
                something_wrong();
            }
            else { //user exists validate entries first
                if((check_username()!='ok') || (check_password()!='ok')){
                    something_wrong();
                }
                else{//all entries are well formatted
                    //set valid_username
                    $valid_username = $username;
                    $_SESSION['valid_user'] = $valid_username;
                    //validate existing user
                    if(validate_existing_user($username,$password)==false){
                        something_wrong();//row with username,pass not found
                    }
                    else {
                        //get user id
                        $user_id = get_user_id($username);
                        if($user_id!=-1){
                            include('w4_view_mainpage.PHP');
                        }

                    }
                }

            }
            $_POST = array();
            $_SESSION = array();
            exit();

在controller.PHP的顶部,它像这样第一次处理着陆在controller.PHP上的情况:

<?PHP
//When controller.PHP is accessed for the first time
if(!isset($_SESSION)){
    session_start();
}
if (empty($_POST['page'])) {
    $d_type = 'none';
    //$_SESSION['display_type'] = $d_type;
    include ('w4_view_startpage.PHP');
    exit();
}

/*
*   When commands come from StartPage or MainPage meaning it's not the first time we landed
on controller.PHP so it goes to the switch statement.
*/

require ('model.PHP');  // connect to MysqL database; functions to access DB tables

// When commands come from StartPage
if ($_POST['page'] == 'StartPage') {
    $command = $_POST['command'];
    $username = $_POST['username'];
    $password = $_POST['password'];
    $email='';
    if(!empty($_POST['email'])){
        $email = $_POST['email'];
    }
.
.
.

包括了更多代码和注释,以显示登录/联接的流程。

  • 因此,第一次将其重定向view_startpage.PHP时,该菜单具有一些可单击的菜单以及用于联接或登录的弹出模式窗口。

  • 成功“加入”后,用户重定向view_startpage.PHP,但是成功登录后,用户重定向view_mainpage.PHP,如上所述,我在其中清除了帖子和会话。

  • 但是当我在Firefox中刷新页面并在PHPStorm上刷新时,$_POST$_REQUEST已经具有pagecommand,{{1} },username,因此我再也无法回到view_startpage.PHP,它只是重新加载password

我尝试过使缓存无效并重新启动,但是它并不总是有效。这一次是在写这篇文章之前。

MysqL的连接以及我在view_mainpage.PHP中的功能似乎都正常工作,因此我不知道该如何做才能“每次重新开始”。

编辑:在下面的评论解决一些问题。

  1. model.PHP$username$password是通过startpage.PHP上的表单提交的。
$email

解决方法

$_POST = array();

清除数组实际上并不会在客户端上清除它,而只是在服务器端-您端。

您需要做的是将用户重定向到同一页面(Location header),这样,请求将从POST更改为GET,并且客户端不会发送以前的信息刷新页面。

例如:

header("Location: ".$_SERVER["PHP_SELF"]);
exit();

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...