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



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

我在 Ubuntu 18.04 上使用 PhpStorm 在调试模式下,为了确保,我已经添加了

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

在登录成功时清除登录页面末尾名为view_mainpage.php的数组,以及在调用exit()之前在controller.php中清除这些数组。

我在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已经有pagecommandusernamepassword的元素,来自以前的登录,所以我永远无法回到view_startpage.php,它只是重新加载view_mainpage.php

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

与MySQL的连接以及我在model.php中的函数似乎都在工作,所以我不知道每次都可以做些什么来"重新开始"。

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

  1. $username$password$email通过 startpage.php 上的表单提交。
<form method='post' action='w4_controller.php'>
<input type='hidden' name='page' value='StartPage'>
<input type='hidden' name='command' value='SignIn'>
<label class='modal-label'>Username:</label>
<input type='text' name='username' required>
<span id="username-error">
<?php
...left out php as it is the homework. basically inline error messages when input is invalid
}?>
</span>
<br>
<br>
<label class='modal-label'>Password:</label>
<input type='password' name='password' required>
<span id="password-error">
<?php
...left out php per above
}?>
</span>
<br>
<br>
<input type='submit'>&nbsp;&nbsp;
<input id='cancel-signin-button' type='button' value='Cancel'>&nbsp;&nbsp;
<input type='reset'>
</form>
$_POST = array();

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

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

前任:

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

最新更新