【问题标题】:Page not redirect after logout in PHP在 PHP 中注销后页面不重定向
【发布时间】:2012-10-04 15:16:16
【问题描述】:

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

【问题讨论】:

  • ”实际上在您的网站上打印了什么?在页面上做“查看源代码”确认

标签: php logout


【解决方案1】:

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

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

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

【讨论】:

  • 如果我的答案有任何用处,请投票赞成,或者如果它对您帮助最大,请接受它,方法是使用投票计数框下方的灰色复选图标 :)
【解决方案2】:

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

session_start();
session_destroy();
header("location:index.php?msg=logout");

【讨论】:

    【解决方案3】:

    试试这样:

    <?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;
      }*/
    }
    ?>
    

    【讨论】:

    • 感谢上面的代码,无论如何我会否决你的帖子。也许其他人.. :)
    猜你喜欢
    • 1970-01-01
    • 2011-10-29
    • 1970-01-01
    • 2014-01-04
    • 2014-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    相关资源
    最近更新 更多