问题描述
仅供参考:这是家庭作业,因此我无法发布所有代码。
我在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
已经具有page
,command
,{{1} },username
,因此我再也无法回到view_startpage.PHP,它只是重新加载password
。
我尝试过使缓存无效并重新启动,但是它并不总是有效。这一次是在写这篇文章之前。
与MysqL的连接以及我在view_mainpage.PHP
中的功能似乎都正常工作,因此我不知道该如何做才能“每次重新开始”。
$email
解决方法
$_POST = array();
清除数组实际上并不会在客户端上清除它,而只是在服务器端-您端。
您需要做的是将用户重定向到同一页面(Location header),这样,请求将从POST更改为GET,并且客户端不会发送以前的信息刷新页面。
例如:
header("Location: ".$_SERVER["PHP_SELF"]);
exit();