2

我有两种情况,目前我正在使用相同的脚本从两个不同的地方运行我的脚本,第一个来自本地主机,第二个来自网站。问题是当我在本地运行它成功注销时,它会重定向到index.php但为什么当我在网站上运行时它不是 100% 工作?注销功能正在工作,但它没有重定向到index.php,它仍然出现在同一页面而不是index.php页面。

我的注销代码如下:

<a href="<?php echo $logoutAction ?>">[Logout]</a>

我的会话代码如下:

<?php
//initialize the session
if (!isset($_SESSION)) {
  session_start();
}

// ** Logout the current user. **
$logoutAction = $_SERVER['PHP_SELF']."?doLogout=true";
if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){
  $logoutAction .="&". htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){
  //to fully log out a visitor we need to clear the session varialbles
  $_SESSION['MM_Username'] = NULL;
  $_SESSION['MM_UserGroup'] = NULL;
  $_SESSION['PrevUrl'] = NULL;
  unset($_SESSION['MM_Username']);
  unset($_SESSION['MM_UserGroup']);
  unset($_SESSION['PrevUrl']);

  $logoutGoTo = "index.php";
  if ($logoutGoTo) {
    header("Location: $logoutGoTo");
    exit;
  }
}
?>
4

3 回答 3

2

最后我自己找到了,我找到的简单代码是:

<?php
session_start(); //Start the current session
session_destroy(); //Destroy it! So we are logged out now
header("location:index.php?msg=logout");
?>

无论如何,感谢所有想帮助我的人。

于 2012-10-14T12:50:10.897 回答
1

试试这样:

<?php

//initialize the session
session_start();

// ** Logout the current user. **
$logoutAction = $_SERVER['PHP_SELF'] . "?doLogout=true";

if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){
  $logoutAction .= "&" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_GET['doLogout'])) &&($_GET['doLogout'] == "true")) {
  //to fully log out a visitor we need to clear the session varialbles
  // $_SESSION['MM_Username'] = NULL;
  // $_SESSION['MM_UserGroup'] = NULL;
  // $_SESSION['PrevUrl'] = NULL;
  // the upper code makes no sense with the code below. just unset the vars, or use "session_destroy"

  unset($_SESSION['MM_Username']);
  unset($_SESSION['MM_UserGroup']);
  unset($_SESSION['PrevUrl']);

  $logoutGoTo = "index.php";
  header("Location: index.php"); // or header("Location: " . $logoutGoTo)

  // no reason checking if $logoutGoTo has any value, cause there's no changing in this code set
  // do not use exit after header!
  /*if ($logoutGoTo) {
    exit;
  }*/
}
?>
于 2012-10-14T12:44:27.827 回答
1

启动新会话并销毁旧会话,如下所示:

session_start();
session_destroy();
header("location:index.php?msg=logout");
于 2017-08-23T14:14:51.537 回答