PHP Timed重定向不重定向?

时间:2014-03-17 00:24:23

标签: php mysql timer

在我的网站上安装此代码,以便在用户注销后将用户重定向回主页并销毁其会话。在我使用我的其他主机帐户来托管该网站之前工作正常,但现在我已经更改了主机,它似乎不再起作用了?它实际上并没有做任何事情。它会破坏会话但不重定向?域名保持不变,一切都让我不明白这里有什么问题?有什么想法吗?

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

3 个答案:

答案 0 :(得分:2)

尝试这个

  echo "<meta http-equiv='refresh' content='0;url=http://www.yoursite.com'>";

OR 在标题调用之前使用flush()

答案 1 :(得分:1)

您之前的托管可能已启用自动输出缓冲。

避免&#34; headers already sent&#34;错误请改变

    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()函数在任何内容的回显之前发生。

答案 2 :(得分:0)

任何输出后都无法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;