我使用以下代码注销用户。我有一段时间没有碰过这段代码了,虽然它在早期工作时完美无缺(当我在我的localhost设置上运行它时仍能正常工作),甚至重定向网页的标题都不起作用这个代码现在。请帮助。
(session_start();
出现在已包含的sar.php文件中。
<?php
$pageTitle = 'Log Out';
require_once('sar.php'); //Contains session_start();
require_once('navig.php');
?>
<div id="wrapper">
<div id="container2">
<div style="min-height: 370px">
<?php
if(isset($_SESSION['u_id'])) {
$_SESSION = array();
if(isset($_COOKIE[session_name()])) {
setcookie(session_name(), '' ,time()-9999);
}
session_destroy();
setcookie('rememberme', '', time()-9999);
setcookie('friend', '', time()-9999);
echo '<h2>You\'ve been logged out successfully.</h2>';
header('Refresh: 1; url= http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']));
}
else {
echo '<h2>You need to be logged in in order to be logged out. Right?</h2><br/>
<h3>Redirecting you to home in 3 seconds. 1.. 2.. 3.. Here you go.';
header('Refresh: 3; url= http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']));
}
?>
</div>
</div>
</div>
<?php
require_once('pair.php');
?>
即使用户未登录,标题也不会重定向页面。当用户登录时,Cookie也不会被清除,正如我从Chrome的开发人员那里看到的那样工具。
上线答案 0 :(得分:0)
试试这个。
<?php
require_once('sar.php'); //Session_start at top position in sar.php is required.
// If you are gonna work with header() use it before you echo out content. Save HTML to variables then echo it out later.
if(isset($_SESSION['u_id']))
{
// Unset both cookie and session arrays.
unset($_SESSION);
unset($_COOKIE);
$content = '<h2>You\'ve been logged out successfully.</h2>';
header("Refresh: 1; url= http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");
}
else
{
$content = '<h2>You need to be logged in in order to be logged out. Right?</h2><br/><h3>Redirecting you to home in 3 seconds. 1.. 2.. 3.. Here you go.';
header("Refresh: 3; url=http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");
}
$pageTitle = 'Log Out';
require_once('navig.php');
?>
<div id="wrapper">
<div id="container2">
<div style="min-height: 370px">
<?php
echo $content;
?>
</div>
</div>
</div>
<?php
require_once('pair.php');
?>