为什么每次运行登录顺序后,我的php$\u POST和$\u会话都没有清除?
仅供参考:这是家庭作业,所以我不能发布所有代码 我在Ubuntu18.04上,在调试模式下使用PhpStorm,为了确保这一点,我添加了为什么每次运行登录顺序后,我的php$\u POST和$\u会话都没有清除?,php,mysql,phpstorm,Php,Mysql,Phpstorm,仅供参考:这是家庭作业,所以我不能发布所有代码 我在Ubuntu18.04上,在调试模式下使用PhpStorm,为了确保这一点,我添加了 $_POST = array(); $_SESSION = array(); 当登录成功时,在名为view\u mainpage.php的登录页末尾清除这些数组,并在调用exit()之前在controller.php中清除这些数组 我在controller.php中有一个用于连接或登录的switch语句。它的一半看起来像这样: switch ($comman
$_POST = array();
$_SESSION = array();
当登录成功时,在名为view\u 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();
<?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'];
}
.
.
.
在controller.php的顶部,它处理第一次登录到controller.php
时的情况如下:
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();
<?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'];
}
.
.
.
清除阵列实际上并不是在客户端清除它,而是在服务器端(您的端)清除它
您需要做的是将用户重定向到同一页面(),这样请求将从POST更改为GET,并且在客户端刷新页面时不会发送以前的信息
例:
清除阵列实际上并不是在客户端清除它,而是在服务器端(您的端)清除它
您需要做的是将用户重定向到同一页面(),这样请求将从POST更改为GET,并且在客户端刷新页面时不会发送以前的信息
例:
如果在脚本末尾清空session变量,我看不到使用session的意义?会话用于在请求之间保存数据,因此在脚本/请求结束时清空数据有点达不到目的。或者出错了吗()
重定向用户?你还应该发布所有相关的代码,比如$username
,$password
等“神奇变量”的来源、表单、数据发送方式(ajax或普通表单提交?)如果有一个“逐步”的指南,你可以重现这个问题。目前还不清楚。如果我们有更广阔的视野,我们可以提供更好的帮助。另外,这仅仅是在PHPStorm中调试时的问题,还是在通过浏览器进行测试时(根本不涉及PHPStorm)存在同样的问题?没有PHPStorm,我的代码就可以工作。我在LEMP堆栈(Nginx,MySQL)上通过终端在VSCode上运行它,没有任何问题。我在PHPStorm上设置了X-debug,我看到它使用cookies,那么这与它有关系吗?或者amit177的答案正确吗?如果在脚本末尾清空session变量,我看不到使用session的意义?会话用于在请求之间保存数据,因此在脚本/请求结束时清空数据有点达不到目的。或者出错了吗()
重定向用户?你还应该发布所有相关的代码,比如$username
,$password
等“神奇变量”的来源、表单、数据发送方式(ajax或普通表单提交?)如果有一个“逐步”的指南,你可以重现这个问题。目前还不清楚。如果我们有更广阔的视野,我们可以提供更好的帮助。另外,这仅仅是在PHPStorm中调试时的问题,还是在通过浏览器进行测试时(根本不涉及PHPStorm)存在同样的问题?没有PHPStorm,我的代码就可以工作。我在LEMP堆栈(Nginx,MySQL)上通过终端在VSCode上运行它,没有任何问题。我在PHPStorm上设置了X-debug,我发现它使用cookies,那么这与它有关系吗?或者amit177的答案正确吗?你能把一个小片段作为你答案的例子吗?补充道,不完全确定它有多有用。P.S.Mangus也是正确的,如果只加载一次会话,然后删除所有数据,那么就没有必要使用会话。您可能需要考虑更改流量的工作方式。例如,不是在登录部分加载页面,而是重定向到另一个具有该页面检查的页面(或同一页面,然后使用会话加载该页面)。显然,这只是一个例子,有很多方法可以让它工作。你是说一个登录页面,检查所有的登录信息是否正确?是的,差不多。如果用户具有有效会话(已登录),请加载主页面/仪表板。否则,请加载登录页面。如果你足够舒服,你也可以把它做成不同的文件。例如,login.php用于登录,index.php用于登录页面(或任何其他页面)。它看起来会更干净,也会给你带来更好的分数:)你能把一个小片段作为你答案的例子吗?补充,不完全确定它会有多大用处。P.S.Mangus也是正确的,如果只加载一次会话,然后删除所有数据,那么就没有必要使用会话。您可能需要考虑更改流量的工作方式。例如,不是在登录部分加载页面,而是重定向到另一个具有该页面检查的页面(或同一页面,然后使用会话加载该页面)。显然,这只是一个例子,有很多方法可以让它工作。你是说一个登录页面,检查所有的登录信息是否正确?是的,差不多。如果用户具有有效会话(已登录),请加载主页面/仪表板。否则,请加载登录页面。如果你足够舒服,你也可以把它做成不同的文件。例如,login.php用于登录,index.php用于登录页面(或任何其他页面)。它看起来会更干净,也会给您带来更好的分数:)
header("Location: ".$_SERVER["PHP_SELF"]);
exit();