代码之家  ›  专栏  ›  技术社区  ›  student0495

注销逻辑后PHP会话未重新初始化

  •  0
  • student0495  · 技术社区  · 6 年前

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

    对于登录,我使用的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.php session_unset 。同样,到目前为止,没有看到任何改善。我一直试图阅读文档,但我的代码似乎没有任何问题,在其他类似的论坛帖子中,我没有发现任何有用的东西。提前感谢您!

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

    1 回复  |  直到 4 年前
        1
  •  0
  •   student0495    6 年前

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