PHP Timed redirect not redirecting?

46 views Asked by At

Have this code on my site to redirect users back to the homepage once logged out and destroys their session. Was working perfectly before when I was using my other hosting account to host the site, but now I've changed the host, it doesn't seem to work anymore ? It isn't actually doing anything. It does destroy session but doesn't redirect? The domain has remained the same and everything so I don't understand what is wrong here? Any ideas?

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

There are 3 answers

2
Arjun Chaudhary On BEST ANSWER

TRY THIS

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

OR use flush() before header call

2
Scuzzy On

Your previous hosting may have had automatic output buffering enabled.

To avoid "headers already sent" errors please change

    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/');

to

    /* 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 />";

And note that the header() function is occurring before the echo of any content.

0
rm-vanda On

You can't do header after any output. There's a setting in the php.ini to change this, but otherwise, it's better practice to send your headers before any output.

But, it looks like you're trying to give them a notification before they get redirected anywhere. To preserve this, just do it with javascript the same way you did on the other one..

     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;