在PHP注销后页面不重定向

时间:2012-10-14 12:32:45

标签: php logout

我有两种情况,目前我使用相同的脚本从两个不同的地方运行我的脚本,首先来自localhost,第二来自网站。问题是当我在本地运行它成功注销时,它会重定向到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 个答案:

答案 0 :(得分:2)

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

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

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

答案 1 :(得分:1)

使用位置标头时应使用完整网址。

假设您需要将index.php附加到主机,请尝试:

$logoutGoTo = 'http://' . $_SERVER['HTTP_HOST'] . '/index.php';

阅读有关header()

的说明

答案 2 :(得分: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;
  }*/
}
?>

答案 3 :(得分:1)

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

api_url