退出按钮没有终止我的当前会话

时间:2019-09-19 05:42:22

标签: php mysql

所以我最近开始在登录和注册页面上工作。完成所有必要的连接后,我意识到我的注销按钮不起作用。

不确定该按钮为什么不起作用,因为单击该按钮时未收到任何错误消息。

我尝试使用其他会话句柄认为它可能有效。

 <?php
    session_start();
    unset($_SESSION['id']);
    session_destroy(); // Destroying All Sessions
    header("Location: ../index.php"); // Redirecting To Home Page
    ?>
  this is my logout.inc.php

那没有任何改变,所以我认为这与我构造表单动作的方式有关。

 <?php
  if (!isset($_SESSION['id'])) {
    echo '<form action="includes/logout.inc.php" method="post">
      <button type="submit" name="logout-submit">Logout</button>
    </form> ';
  }
  else if (isset($_SESSION['id'])) {
    echo '<form action="includes/login.inc.php" method="post">
      <input type="text" name="mailuid" placeholder="E-mail/Username">
      <input type="password" name="pwd" placeholder="Password">
      <button type="submit" name="login-submit">Login</button>
    </form>
    <a href="signup.php" class="header-signup">Signup</a>';
  }
   ?>

我想,问题出在我的登录脚本上

<?php

if (isset($_POST['login-submit'])) {
  require 'dbh.inc.php';
  $mailuid = $_POST['mailuid'];
  $password = $_POST['pwd'];

  if (empty($mailuid) || empty($password)) {
    header("Location: ../index.php?error=emptyfields");
    exit();
  }
  else {
    $sql = "SELECT * FROM users where uidUsers=? OR emailUsers=?;";
    $stmt = mysqli_stmt_init($conn);
    if (!mysqli_stmt_prepare($stmt, $sql)){
      header("Location: ../index.php?error=sqlerror");
      exit();
    }
    else {
      mysqli_stmt_bind_param($stmt, "ss", $mailuid, $mailuid);
      mysqli_stmt_execute($stmt);
      $result = mysqli_stmt_get_result($stmt);
      if ($row = mysqli_fetch_assoc($result)) {
        $pwdCheck = password_verify($password, $row['pwdUsers']);
        if ($pwdCheck == false) {
          header("Location: ../index.php?error=wrongpassword");
          exit();
        }
        else if ($pwdCheck == true) {
          session_start();
          $_SESSION['userId'] = $row['idUsers'];
          $_SESSION['userUid'] = $row['uidUsers'];
          header("Location: ../index.php?login=success");
          exit();
        }
        else {
          header("Location: ../index.php?error=wrongpassword");
          exit();
        }
      }
      else {
        header("Location: ../index.php?error=nouser");
        exit();
      }
    }
  }
}
else {
  header("Location: ../index.php");
  exit();
}

?>

但是即使那样,我也不知道从哪里开始,所以我希望有人可以帮助找到我错过的东西或需要更改的东西。

我的预期输出是,单击“注销”按钮将完全终止当前会话并开始一个新会话,这又会将我转移到登录/注册页面。

    <?php

  require "header.php";
?>

    <main>
      <div class="wrapper-main">
        <section class="section-default">
          <!--
          We can choose whether or not to show ANY content on our pages depending on if we are logged in or not.
          -->
          <?php
          if (!isset($_SESSION['userId'])) {
            echo '<p class="login-status">You have Successfully logged In!</p>';
          }
          else if (isset($_SESSION['userId'])) {
            echo '<p class="login-status">You are logged Out!</p>';
          }
          ?>
        </section>
      </div>
    </main>

<?php
  // And just like we include the header from a separate file, we do the same with the footer.
  require "footer.php";
?>
this is the index.php file

3 个答案:

答案 0 :(得分:0)

在logout.inc.php中尝试此代码

if(isset($_POST['logout-submit']))
{
 unset($_SESSION['userId']);
 header("Location: ../index.php");  
}

答案 1 :(得分:0)

logout.inc.php

中尝试以下操作
session_start();
session_destroy();

foreach($_SESSION as $k => $v) {
   unset($_SESSION[$k]);
   session_unset($k);
}

header('Location: '../index.php');

答案 2 :(得分:0)

您不需要表单来专门发布它,以这种方式仍然可以完成,更好的解决方案是将注销网址保留在<a>中。

这将重定向到该页面,然后删除/破坏您的会话。

<?php
  if (!isset($_SESSION['id'])) {
    echo '<a href=includes/logout.inc.php>Logout</a>';
  }
  else if (isset($_SESSION['id'])) {
    echo '<form action="includes/login.inc.php" method="post">
      <input type="text" name="mailuid" placeholder="E-mail/Username">
      <input type="password" name="pwd" placeholder="Password">
      <button type="submit" name="login-submit">Login</button>
    </form>
    <a href="signup.php" class="header-signup">Signup</a>';
  }
?>

logout.inc.php

<?php 
session_start();
unset($_SESSION['id']);
session_destroy(); // Destroying All Sessions
header("Location: ../index.php"); // Redirecting To Home Page
?>

index.php

<?php 
if(!isset($_SESSION['id']))
{
    header("Location: login.php"); //redirect ot login page
}
else
{
    //do your operations
}
?>

我担心您输入的登出路径错误,如果正确,请尝试对其进行调试,以检查它是否实际上指向此页面,然后,如果是破坏会话的检查,它会重定向回正确的页面。