简体   繁体   English

在 PHP 中注销后页面不重定向

[英]Page not redirect after logout in PHP

I have two situation, currently I'm run my script from two different places with same script, first from localhost and second from website.我有两种情况,目前我使用相同的脚本从两个不同的地方运行我的脚本,第一个来自本地主机,第二个来自网站。 The problem is when I run locally it logout successfully, it will redirect to index.php but why when I run at website it's not 100% working?问题是当我在本地运行它成功注销时,它会重定向到index.php但是为什么当我在网站上运行它不是 100% 工作? The logout function is working but it's not redirect to index.php , It still appear the same page not index.php page.注销功能正在工作,但它没有重定向到index.php ,它仍然显示相同的页面而不是index.php页面。

My Logout Code as below:我的注销代码如下:

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

My Session Code as below:我的会话代码如下:

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

Finally, I found it by myself, the simple code that I found is:最后我自己找到了,我找到的简单代码是:

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

Anyway, thanks to all of you that want to try to help me.无论如何,感谢所有想帮助我的人。

You should use the full url when using a location header. 使用位置标头时,应使用完整网址。

Assuming index.php is all you need to append to the host, try: 假设只需要将index.php附加到主机,请尝试:

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

Read the notes about header() 阅读有关header()的注释

Try it like this:像这样尝试:

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

start new one and destroy old session , like following:启动新会话并销毁旧会话,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM