【问题标题】:PHP session not reinitializing after logout logic注销逻辑后PHP会话未重新初始化
【发布时间】:2018-09-12 02:10:12
【问题描述】:

我的会话变量有问题,到目前为止我一直使用它,但是在实现注销逻辑后,重新登录后我无法再次存储我的会话变量。

对于登录,我使用如下所示的 ajax 请求:

if ($row['password'] == $entered_password) {
      if (!isset($_SESSION['user_email'])) {
          $_SESSION['user_email'] = $entered_email;
      } else {
          unset($_SESSION['user_email']);
          $_SESSION['user_email'] = $entered_email;
      }
      echo "login_validated";
      exit;
    } else {
      echo "invalid_password";
      exit;
    }

请求是:

$.post('php/login.php', {
                  emailLogin: emailLogin,
                  passwordLogin: passLogin
                }, function (responseText) {
                    if (responseText === "invalid_username") {
                        alert ("Username is invalid! Please check it again or make sure you have already registered first!");
                    } else if (responseText === "invalid_password")  {
                        alert ("Given password is incorrect! Please try again.");
                    } else if (responseText === "login_validated") {
                        window.location.href = "php/pages/expenses.php";

                    } else {
                        console.log(responseText);
                        alert ("A problem occured at te server level, please try again later! If problem persists, please contact us!");
                    }
                });

但在实现并使用以下逻辑注销后,我的会话变量值不再保存和显示:

 $(document).ready( function (event){
            $('#logoutButton').click(function (event) {
                event.preventDefault();
                var user_response = confirm("Are you sure you want to logout? Your current session will be closed!");
                if (user_response === true) {
                    <?php
                        if (isset($_SESSION['user_email'])) {
                            unset($_SESSION['user_email']);
                        }                            
                        session_destroy();
                    ?>
                    window.location.href = "../../index.php";
                }
            });
        });

我提到我首先尝试使用单独的文件进行带有标头重定向的注销,但被我内置的 adblocker similar ad-blocker error 阻止。我想也许在我以前的登录操作中,我做了太多的会话变量,并开始清理我所有的 cookie。它没有任何效果。另外,阅读其他帖子和文档,仍然不知道我做错了什么。

另外,关于确保清除所有以前存储的会话变量,我调用了一次函数:http://php.net/manual/ro/function.session-unset.phpsession_unset。同样,到目前为止没有看到任何改善。我一直在尝试阅读文档,但我的代码似乎没有任何问题,在另一个类似的论坛帖子中,我没有发现任何有用的东西。提前谢谢!

编辑:关于密码的简短说明 - 是的,目前它们以明文形式存储,但这只是一个个人项目,完成后我还将对密码实施盐和胡椒加密。

【问题讨论】:

  • 您正在混合客户端和服务器端代码。 unset()session_destroy() 在“页面生成”(在服务器上执行)时完成,而不是在单击按钮时完成。
  • 我实际上有点担心这一点,但我认为只有在单击按钮时才会调用 unset ...我现在将使用对单独 logout.php 的简单请求进行重构文件并告诉你结果。

标签: php login session-variables logout


【解决方案1】:

非常感谢@Syscall!几乎疯了 :) 保持一切不变,只是将前端内的 php 脚本修改为 ajax 请求:

`var user_response = confirm("Are you sure you want to logout? Your current session will be closed!");
if (user_response === true) {
$.ajax({url: "../logout.php", success: function(result){
console.log(result);
window.location.href = "../../index.php";
}});
}`

还在注销文件中添加了session_start();。现在所有的逻辑都工作了,登录、注销、尝试不同的用户等等。

【讨论】:

    猜你喜欢
    • 2014-02-23
    • 2010-09-24
    • 2013-10-14
    • 2018-09-09
    • 1970-01-01
    • 2015-03-30
    • 1970-01-01
    • 2013-08-13
    • 1970-01-01
    相关资源
    最近更新 更多