0

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;
    }
}
?>
AidanPT
  • 185
  • 1
  • 3
  • 12

3 Answers3

2

TRY THIS

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

OR use flush() before header call

Arjun Chaudhary
  • 2,373
  • 2
  • 19
  • 36
  • This worked for me! I guess using a HTML version is the only way it's going to be reliable? Thanks for your help! – AidanPT Mar 17 '14 at 00:37
  • its ok actually they are correct after an o/p u dont use header i develop website it was big headache for me but now i also have started doing this – Arjun Chaudhary Mar 17 '14 at 00:41
1

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.

Community
  • 1
  • 1
Scuzzy
  • 12,186
  • 1
  • 46
  • 46
  • Still not working, I used the same Header code on another empty page and it worked fine? For some reason it doesn't seem to be initiated on the logout page? – AidanPT Mar 17 '14 at 00:35
  • Then this mustn't be the issue, are you able to inspect your http request/response headers in your browser to confirm that the client is receiving them? Otherwise all I can suggest is adding a space character after the `3;` to become `3 ;` – Scuzzy Mar 17 '14 at 00:50
0

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; 
rm-vanda
  • 3,122
  • 3
  • 23
  • 34