在 php 中注销和重定向会话
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4608182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
logout and redirecting session in php
提问by Dinesh Kumar
the below one is the link in my php site.. after clicking this button the user's session should be terminated and he should be redirected again to the home page.. i have written the coding for this concept as follows but it shows me only a blank page(it is not redirected to the home page).. please correct my codings
下面是我的 php 站点中的链接.. 单击此按钮后,用户的会话应该终止,他应该再次重定向到主页.. 我已经为这个概念编写了如下代码,但它只显示了一个空白页(它不会重定向到主页).. 请更正我的编码
<a href="Logout.php">
click here to log out</a>
codings in the Logout.php a follows
Logout.php 中的编码如下
<?
session_start();
session_unset();
session_destroy();
ob_start();
header("location:home.php");
ob_end_flush();
include 'home.php';
//include 'home.php';
exit();
?>
回答by deepcell
Only this is necessary
只有这个是必要的
session_start();
unset($_SESSION["nome"]); // where $_SESSION["nome"] is your own variable. if you do not have one use only this as follow **session_unset();**
header("Location: home.php");
回答by Poelinca Dorin
Use this instead:
改用这个:
<?
session_start();
session_unset();
session_destroy();
header("location:home.php");
exit();
?>
回答by Adam Prax
<?php
session_start();
session_destroy();
header("Location: home.php");
?>
回答by Datu Taklob
<?php
session_start();
session_unset();
session_destroy();
header("location:home.php");
exit();
?>
回答by alex
<?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 variables
$_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;
}
} ?>