Logout.php不起作用

时间:2017-02-03 20:36:24

标签: php logout

<?php
     session_start();
     if (!isset($_SESSION['korisnik'])) {
      header("Location: index.php");
     } else if(isset($_SESSION['korisnik'])!="") {
      header("Location: home.php");
     }

     if (isset($_GET['Odjava'])) {
      unset($_SESSION['korisnik']);
      session_unset();
      session_destroy();
      header("Location: index.php");
      exit();
     }
     ?>

每次按logout时,home.php都会刷新,会话还没有结束。

4 个答案:

答案 0 :(得分:0)

<?php

 if (isset($_GET['Odjava'])) {
  unset($_SESSION['korisnik']);
  session_unset();
  session_destroy();
  header("Location: index.php");
  exit();
 }

 session_start();
 if (!isset($_SESSION['korisnik'])) {
  header("Location: index.php");
 } else if(isset($_SESSION['korisnik'])!="") {
  header("Location: home.php");
 }
 ?>

尝试此操作,因为您需要先检查它是否已设置,否则您的脚本将重定向,因为您的if语句位于会话销毁之上

答案 1 :(得分:0)

更好地使用PHP documentation方法:

<?php
// Initialize the session.
// If you are using session_name("something"), don't forget it now!
session_start();

// Unset all of the session variables.
$_SESSION = array();

// If it's desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
if (ini_get("session.use_cookies")) {
    $params = session_get_cookie_params();
    setcookie(session_name(), '', time() - 42000,
        $params["path"], $params["domain"],
        $params["secure"], $params["httponly"]
    );
}

// Finally, destroy the session.
session_destroy();
?>

为了也删除会话cookie。

答案 2 :(得分:0)

对我来说,这就是窍门:

setcookie(session_name(), session_id(), 1); 
$_SESSION = [];

即首先使会话过期, 然后清除$ _SESSION变量。

答案 3 :(得分:-1)

首先运行第一个if块,并且当时仍然设置会话。颠倒if块的顺序,你可能会得到更好的结果。