session_destroy 无法销毁会话 php

session_destroy cannot destroy session php

我有登录和注销页面,但无法注销,总是说用户已经登录。这是我的登录页面:

<?php


 session_start();
  include_once("connection.php");
  if(isset($_POST) & !empty($_POST)) {
    $userName = mysqli_real_escape_string($connection, $_POST['userName']);
    $userPassword = md5($_POST['userPassword']);
    $login = "SELECT * FROM `users` WHERE userName = '$userName' and password = '$userPassword'";
    $result = $connection->query($login);
    while ($val = mysqli_fetch_array($result))
    {
        $isAdmin    = $val['isAdmin'];
        $companyID  = $val['companyID'];
        $branchID   = $val['branchID'];
        $ID         = $val['ID'];
    }
    $count = mysqli_num_rows($result);
    if($count == 1){
      $_SESSION['userName'] = $userName;
      setcookie("userID", $ID);
      setcookie("companyID", $companyID);
      setcookie("branchID", $branchID);
      if(!$isAdmin){
        header('location: home.php');
      }
      else {
        header('location: admin/home.php');
      }
    }
    else {
      $fmsg = "Wrong user name";
    }
  }
  if(isset($_SESSION['userName'])){
    $smsg = "Already loggedin";
  }
?>

这是注销页面:

<?php
  session_start();
  session_destroy();
  unset($_COOKIE['companyID']);
  unset($_COOKIE['userID']);
  unset($_COOKIE['branchID']);
  setcookie('companyID', null, -1, '/');
  setcookie('userID', null, -1, '/');
  setcookie('branchID', null, -1, '/');
  header('location: index.php');
?>

我该如何解决这个问题?谁能帮帮我?还有一张图片显示饼干。

这里你可以做的是

 $_SESSION['userName'] = ''; //clear userName only

并将其添加到您的代码中

if(isset($_SESSION['userName']) && $_SESSION['userName'] != ''){
    $smsg = "Already loggedin";
  }

unset($_SESSION); //all session will be gone
$_SESSION = array();


// unset cookies
if (isset($_SERVER['HTTP_COOKIE'])) {
    $cookies = explode(';', $_SERVER['HTTP_COOKIE']);
    foreach($cookies as $cookie) {
        $parts = explode('=', $cookie);
        $name = trim($parts[0]);
        setcookie($name, '', time()-1000);
        setcookie($name, '', time()-1000, '/');
    }
}

希望对您有所帮助

就用这个:

$_SESSION = array();  
session_destroy();

这是我的代码,它对我有用

session_start();
unset($_SESSION['userid']);
unset($_SESSION['username']);
unset($_SESSION['photo']);
session_destroy();
unset($_COOKIE['userid']);
unset($_COOKIE['emailid']);
unset($_COOKIE['photo']);
setcookie('userid', null, -1, '/');
setcookie('emailid', null, -1, '/');
setcookie('photo', null, -1, '/');
header("Location:login");

我认为问题出在与 MS Edge 配合使用的 Firefox。感谢您的所有帮助,我的代码在这里: 注销:

<?php
  session_start();
  setcookie("branchID", "", 1);
  setcookie("companyID", "", 1);
  setcookie("userID", "", 1);
  setcookie("userName", "", 1);
  session_destroy();
  header('location: index.php');
?>

登录:

<?php


session_start();
  include_once("connection.php");
  if(isset($_POST) & !empty($_POST)) {
    $userName = mysqli_real_escape_string($connection, $_POST['userName']);
    $userPassword = md5($_POST['userPassword']);
    $login = "SELECT * FROM `users` WHERE userName = '$userName' and password = '$userPassword'";
    $result = $connection->query($login);
    while ($val = mysqli_fetch_array($result))
    {
        $isAdmin    = $val['isAdmin'];
        $companyID  = $val['companyID'];
        $branchID   = $val['branchID'];
        $ID         = $val['ID'];
    }
    $count = mysqli_num_rows($result);
    if($count == 1){
      $_SESSION['userName'] = $userName;
      setcookie("userID", $ID);
      setcookie("companyID", $companyID);
      setcookie("branchID", $branchID);
      if(!$isAdmin){
        header('location: home.php');
      }
      else {
        header('location: admin/home.php');
      }
    }
    else {
      $fmsg = "Hatalı Kullanıcı Adı/Şifre";
    }
  }
  if(isset($_SESSION['userName']) && $_SESSION['userName'] != ''){
    $smsg = "Kullanıcı Halihazırda Giriş Yapmış";
  }
?>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="css/style.css">
    <link rel="stylesheet" href="css/lib/lobipanel/lobipanel.min.css">
    <link rel="stylesheet" href="css/lib/jqueryui/jquery-ui.min.css">
    <link rel="stylesheet" href="css/lib/font-awesome/font-awesome.min.css">
    <link rel="stylesheet" href="css/main.css">
  </head>
  <body>
    <header class="site-header">
        <div class="container-fluid">
            <a href="#" class="site-logo">
                <img class="hidden-md-down" src="img/logo-2.png" alt="">
                <img class="hidden-lg-up" src="img/logo-2-mob.png" alt="">
            </a>
            <div class="site-header-content">
                <div class="site-header-content-in">
                    <div class="site-header-shown">
                      <a href="#" class="site-logo">
                          <img class="hidden-md-down" src="img/logo-ds.png" alt="">
                          <img class="hidden-lg-up" src="img/logo-ds-mob.png" alt="">
                      </a>
                    </div><!--.site-header-shown-->
                    HOŞ GELDİNİZ
                </div><!--site-header-content-in-->
            </div><!--.site-header-content-->
        </div><!--.container-fluid-->
    </header><!--.site-header-->
    <div class="form-style-6">
      <h1>Giriş Yap</h1>
      <form action="" method="post">
        <input type="text" name="userName" placeholder="Kullanıcı Adı" />
        <input type="password" name="userPassword" placeholder="Şifre" />
        <input type="submit" value="Giriş Yap" />
      </form>
    </div>
    <div class="container">
      <?php if(isset($smsg)){ ?>
        <div class="alert alert-success" role="alert"> <?php echo $smsg; ?></div>
      <?php } ?>
      <?php if(isset($fmsg)){ ?>
        <div class="alert alert-danger" role="alert"> <?php echo $fmsg; ?></div>
      <?php } ?>
      <?php if(isset($asmsg)){ ?>
        <div class="alert alert-success" role="alert"> <?php echo $asmsg; ?></div>
      <?php } ?>
      <?php if(isset($afmsg)){ ?>
        <div class="alert alert-danger" role="alert"> <?php echo $afmsg; ?></div>
      <?php } ?>
    </div>
  </body>
</html>