PHP session 注销逻辑后不重新初始化

PHP session not reinitializing after logout logic

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

对于登录,我使用如下所示的 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!");
                    }
                });

但是在执行并使用以下注销逻辑后,我的 session 变量值不再保存和显示:

 $(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";
                }
            });
        });

我提到我首先尝试使用单独的文件通过 header 重定向进行注销,但被我内置的广告拦截器 similar ad-blocker error 阻止了。我想也许在我之前的登录操作中我做了太多 session 变量,然后继续清理我所有的 cookie。它没有任何效果。另外,阅读其他帖子和文档,仍然不知道我做错了什么。

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

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

非常感谢@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();。现在所有的逻辑都正常了,登录、注销、尝试不同的用户等等。