session_destroy无法破坏会话php

时间:2017-06-01 11:16:48

标签: php session session-cookies destroy

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

<?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";
  }
?>

这是loggout页面:

<?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');
?>

我怎么能解决这个问题?谁能帮助我?还有附加图像显示cookie。enter image description here

4 个答案:

答案 0 :(得分:2)

请使用:

$_SESSION = array();  
session_destroy();

答案 1 :(得分:1)

你可以做的就是

 $_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, '/');
    }
}

希望这有帮助

答案 2 :(得分:0)

这里我的代码是我的工作

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");

答案 3 :(得分:0)

我认为Firefox的问题在于MS Edge。感谢您的帮助我的代码在这里: 注销:

<?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>

登录:

RuleFor(m => m.State.TrueForAll(x => x.Length == 2)).Equals(true);