Jump to content

Recommended Posts

Does anyone know why clicking the 'Log Out' button created in this file:

<div class="widget">
<h1>Hello, <?php echo $user_data['first']; ?>!</h1>
     <div class="inner">
     	<nav id="side-links">
          	<li><a href="logout.php">Log Out</a></li>
               <li><a href="changepassword.php">Change Password</a></li>
          </nav>
     </div>
</div>

 

is not logging the user out? Here is logout.php:

<?php 
session_start();
session_destroy();
header('Location: index.php');
?>

 

I can click the log out button and see the page refresh but instead of replacing my Logged In information with a simple Log In form, it just leaves my info there as if I'm still logged in. The code that controls that:

<aside class="widgets">
     	<?php
	if (logged_in() === true) {
		include 'includes/widgets/loggedin.php';
	} else {
		include 'includes/widgets/login.php';
	}
	?>
     </aside>

 

Can someone explain why this is happening and how to fix it?

Link to comment
https://forums.phpfreaks.com/topic/264614-logout-not-logging-out/
Share on other sites

There's more to killing the session data associated with the login than just calling session_destroy(). Have you read the manual entry? There's a lot of useful information that helps to explain why this is happening. http://php.net/manual/en/function.session-destroy.php

This still has my failed attempted at troubleshooting  in it as well...

<?php 
session_start();

$_SESSION = array();
if (ini_get("session.use_cookies")) {
    $params = session_get_cookie_params();
    echo $params;
    die();
    
    setcookie(session_name(), '', time() - 42000,
        $params["path"], $params["domain"],
        $params["secure"], $params["httponly"]
    );
} else {
die('Didn\'t Work');
}
session_destroy();
//header('Location: index.php');
?>

Because you are redirecting around all over the place, I suspect you have more than one session going on, either because you are switching back and forth between having and not having the www. on the url and/or you are changing paths in the url. See the following post (concerns trying twice to log in, but might be related to your not being able to log out) http://forums.phpfreaks.com/index.php?topic=360649.msg1705611#msg1705611

 

Are all your URL's consistent, i.e. all with or all without the www. on them? What does a phpinfo() statement show for the session.cookie_path setting?

if this is your last code:

<?php 
session_start();

$_SESSION = array();
if (ini_get("session.use_cookies")) {
    $params = session_get_cookie_params();
    echo $params;  // REMOVE IT
    die(); // REMOVE IT
    
    setcookie(session_name(), '', time() - 42000,
        $params["path"], $params["domain"],
        $params["secure"], $params["httponly"]
    );
} else {    // REMOVE THIS LINE
die('Didn\'t Work'); // AND THIS TOO
}
session_destroy();
//header('Location: index.php');  // AND MAYBE YOU NEED THIS LINE TO BE ACTIVE 
?>

 

did you try removing completely that last else sentence (look the comments in your code) and see what happens?

 

 

So the session.cookie_path is set to "/"

 

That would mean that a changing path in the URL isn't causing multiple different sessions to exist for one client (browser.)

 

The problem can still be due to URL's that are changing back and forth between having and not having www. in them.

 

What does the phpinfo() statement show for the session.cookie_domain? (it's very likely empty as that is the default value and it would require you to specifically be setting it.)

 

Since we don't have all your code needed to reproduce this problem, you would need to debug exactly what is occurring. For your logout code to clear the same session data that corresponds to your log in code, you need to have the same session id on the log out page that you have on your log in page (and in the session id cookie in your browser.) You can echo session_id(); in your code to see what the current session id is. You can also check the session id cookies in your browser to see if there is more than one matching your base domain, there would be one cookie for www.yourdomain.com and one for just yourdomain.com

I hadn't but it was because I was trying to see if the if statement was even executing. Neither die() statement executed so I changed the code to this:

<?php 
session_start();
unset($_SESSION['id']);
$_SESSION['id'] = 0;
session_destroy();
header('Location: index.php');
?>

 

It worked once but once I logged in again to verify that it really did do the trick, I was unsuccessful in logging out again.

Last guess, without seeing all the code needed to reproduce the problem - you have some code in index.php or in includes/widgets/login.php that is setting $_SESSION['id']. Probably in a conditional test that only has one = sign (an assignment operator) instead of two == signs (an equal comparison operator.)

 

If that's not it, you will need to post all the code (less any database credentials) that reproduces the problem.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.