在我的网站上有这段代码,一旦用户注销并破坏他们的会话,将用户重定向回主页。当我使用我的其他托管帐户来托管网站时,它工作得很好,但现在我已经更改了主机,它似乎不再工作了?它实际上什么也没做。它会销毁会话,但不会重定向?域名保持不变,所以我不明白这里出了什么问题?什么好主意吗?
<?
session_start();
if(!isset($_REQUEST['logmeout'])){
echo "<strong><font color=green>Are you sure you want to logout?</font></strong><br />";
echo "<a href=logout.php?logmeout>Yes</a> | <a href=javascript:history.back()>No</a>";
}
else {
session_destroy();
if(!session_is_registered('first_name')){
echo "<center><font color=red><strong>You have now been logged out.</strong></font></center><br />";
echo "<center>You will be redirected in 3 second...</center><br />";
/* Redirect browser */
header('Refresh: 3;url=http://www.basecentre.co.uk/');
/* Make sure that code below does not get executed when we redirect. */
exit;
}
}
?>
TRY THIS
echo "<meta http-equiv='refresh' content='0;url=http://www.yoursite.com'>";
或在header调用前使用flush()
您以前的主机可能已经启用了自动输出缓冲。
要避免"headers already sent"错误,请修改
echo "<center><font color=red><strong>You have now been logged out.</strong></font></center><br />";
echo "<center>You will be redirected in 3 second...</center><br />";
/* Redirect browser */
header('Refresh: 3;url=http://www.basecentre.co.uk/');
/* Redirect browser */
header('Refresh: 3;url=http://www.basecentre.co.uk/');
echo "<center><font color=red><strong>You have now been logged out.</strong></font></center><br />";
echo "<center>You will be redirected in 3 second...</center><br />";
注意,header()
函数发生在任何内容的回显之前。
您不能在任何输出后执行header
。php.ini中有一个设置可以改变这一点,但除此之外,最好在输出之前发送头信息。
但是,看起来你想在他们被重定向到任何地方之前给他们一个通知。为了保持这一点,只需用javascript做同样的方式你做了另一个。
echo "<center><font color=red><strong>You have now been logged out.</strong></font></center><br />";
echo "<center>You will be redirected in <span id="time">3<span> second...</center><br />";
//And then echo the redirect script.
echo <<<JAVASCRIPT
<script>
var count = 3;
var counter = setInterval(timer, 1000);
function timer() {
count = count - 1;
if (count <= 0) {
window.location.pathname = '/user/index';
}
document.getElementById("time").innerHTML = count;
}
window.onload = timer();
</script>
JAVASCRIPT;