0

I've the following code to log out an user. I haven't touched this code since a while, and while it was working flawlessly earlier (and still works as expected when I run it on my localhost setup), even the header to redirect the webpage isn't working in this code now. Help please.

(session_start(); is present in the sar.php file which has been included.

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

The header doesn't redirect the page even if the user is not logged in. When the user is logged in, the cookies aren't cleared either, as I can see from Chrome's developer tools.

Edit: This page is online at http://www.recharged.in/logout.php

  • U can't produce output if u'r using `header()` (http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) – DarkBee Sep 30 '14 at 11:23
  • @DarkBee Headers can be called after a session is initalized, there is nothing wrong here. If – Daryl Gill Sep 30 '14 at 11:26
  • But the exact same code was working fine till now, and still works on my localhost setup? – Gaurav Gahlyan Sep 30 '14 at 11:27
  • @DarylGill watch the output before the `header` – DarkBee Sep 30 '14 at 11:27
  • I've tried removing the header completely. But the cookies and session variable still isn't getting cleared. – Gaurav Gahlyan Sep 30 '14 at 11:30
  • Hi, I see you are redirecting to the same page after destroying your session.. After using session_destroy(), the session cookie is removed and the session is no longer stored on the server. The values in $_SESSION may still be available, but they will not be on the next page load. try unset($_SESSION) for testing purpose only.because unsetting the session will remove all session from the server. – justrohu Sep 30 '14 at 11:34
  • Hi, thanks for the suggestion but I've already tried `unset($_SESSION)`. And it isn't redirecting to the same page, it is redirecting to the home page of the website. – Gaurav Gahlyan Sep 30 '14 at 11:38
  • Also make sure `session_start();` is at the topmost position in your code. – Rimble Sep 30 '14 at 12:05

1 Answers1

0

Try this.

<?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');
?>
Rimble
  • 873
  • 8
  • 22