Jump to content

Closing session for good on logout


jwhite68

Recommended Posts

I am trying to ensure the session itself is killed on my logout procedure.

 

This is a segment from the end of my logout.php:

 

if (isset($_COOKIE[session_name()])) {
    setcookie(session_name(), '', time()-42000, '/');}

session_destroy();
header("Location: goodbye.php");

 

Basically, after the user has logged out, I want the session id to be deleted.  The script then refers them to goodbye.php.  goodbye.php has a hyperlink back to the login page.

 

The problem is, when I click the hyperlink which takes me back to the login page, I still see the same session id (when i output the session info).  My goodbye.php script does not have session_start() in it anywhere.

 

Does anyone know if I am doing something wrong above?

Link to comment
Share on other sites

Moving my session_start to the top didnt help.

 

In your script, if you try to view the session info, do you see the same session id?

 

I have read that session_destroy only deletes the session variables, but does not delete the session_id itself.

Link to comment
Share on other sites

Exactly - thats the page I am referring to. My code uses the sample code in here to try and delete the session_id (ie the cookie).  But this simply doesnt work.

 

When I am referred back to the login page, and have an echo for the session_id - its still showing the same value as for the previously logged in session.

Link to comment
Share on other sites

I also tried this:

 

unset($_SESSION['PHPSESSID']); 
if (isset($_COOKIE[session_name()])) {
    setcookie(session_name(), '', time()-42000, '/');}

 

Doesnt help. 

 

I cant understand how the same session can still be shown, when I have passed control via a page that doesnt have session_start().

Link to comment
Share on other sites

But this header redirect takes it to a page (called goodbye.php) - which just has a hyperlink on it that takes it back to the login page (via a href). I dont see how changing just the header redirect to javascript will work.

Link to comment
Share on other sites

Doesn't matter if you can't see how.

 

Sometimes headers and sessions do not get along. For instance I bet if you tried setting a session variable than do a header redirect to a page that should display that value it probably would not work.

 

Give that a try and see if I am right, if I am than the problem lies within the header redirect. If I am not, than well we know it is not the header causing the issue.

Link to comment
Share on other sites

<?php
session_start();
if (isset($_COOKIE[session_name()])){setcookie(session_name(), '', time()-42000, '/');}
session_destroy();
echo("<script type='text/javascript'>parent.location='./goodbye.php'</script>");
?>

 

Try it takes two seconds.. chances are frost110 is right ;)

Link to comment
Share on other sites

<?php
session_start();
session_destroy();
echo '<script type="text/javacsript">window.location=\'http://www.yoursite.com/path/to/goodbye.php\';</script>';
?>

 

Replace the yoursite and path with the full path to goodbye.php see if that works.

Link to comment
Share on other sites

Try this:

 

<?php
  session_start();
  session_unset();
  session_destroy();
  $_SESSION = Array();
?>

 

Also, I don't see the point in "good bye" or "you have been logged out" messages with a link back to the home page.  Why not just redirect back to the home page, the user will know they've logged out when they're faced with the log in screen again.

Link to comment
Share on other sites

Sorry that didnt work either.

 

Initially, my logout did take the user back to the home page.  However, because I saw the same session_id there - I thought that directing to a separate page with no session_start() would solve the problem, but it didnt.  So, jumping to a 'good bye' page was really a workaround for the problem.  As it happens, it hasnt worked anyway.

 

So if I can get to the bottom of this, I hope to revert back to just returning to login page (home page) via header redirect.

 

 

Link to comment
Share on other sites

Here the full logout.php:

 

<?php
   session_start();
   include("dbconnect.php");	

// track logout time in statistics if user logged in
if($_SESSION['online'] && $_SESSION['LoginStatus']){	

@mysql_query("UPDATE stats_ppl_online 
         SET logout_time=now() 
         WHERE session_id='".session_id()."'");
}

// kill session
$_SESSION['LoginStatus'] = '';

unset($_SESSION['PHPSESSID']); 
session_unset();
//if (isset($_COOKIE[session_name()])) {
//    setcookie(session_name(), '', time()-42000, '/');}
session_destroy();
$_SESSION = array();
//header("Location: goodbye.php");
echo("<script type='text/javascript'>parent.location='goodbye.php'</script>");

//exit();
?>

Link to comment
Share on other sites

What does this output:

<?php
   session_start();
   include("dbconnect.php");	

   // track logout time in statistics if user logged in
   if($_SESSION['online'] && $_SESSION['LoginStatus']){	
@mysql_query("UPDATE stats_ppl_online 
         SET logout_time=now() 
         WHERE session_id='".session_id()."'");
    }

echo "<pre style=\"text-align: left;\">" . print_r($_SESSION, true)
     . "</pre>";			 
session_unset();
session_destroy();
$_SESSION = array();
echo "<pre style=\"text-align: left;\">" . print_r($_SESSION, true)
     . "</pre>";			 
?>

Link to comment
Share on other sites

<?php
   session_start();
   include("dbconnect.php");	

// track logout time in statistics if user logged in
if($_SESSION['online'] && $_SESSION['LoginStatus']){	

@mysql_query("UPDATE stats_ppl_online 
         SET logout_time=now() 
         WHERE session_id='".session_id()."'");
}

// kill session
foreach ($_SESSION as $key => $val) {
     $_SESSION[$key] = null;
     unset($_SESSION[$key];
}

if (isset($_COOKIE[session_name()])) {
    setcookie(session_name(), '', time()-42000, '/');
}

session_destroy();

//header("Location: goodbye.php");
echo("<script type='text/javascript'>parent.location='goodbye.php'</script>");

//exit();
?>

 

Try that out and see where it gets you.

Link to comment
Share on other sites

<?php
   session_start();
   include("dbconnect.php");	

// track logout time in statistics if user logged in
if($_SESSION['online'] && $_SESSION['LoginStatus']){	

@mysql_query("UPDATE stats_ppl_online 
         SET logout_time=now() 
         WHERE session_id='".session_id()."'");
}

// kill session
foreach ($_SESSION as $key => $val) {
     $_SESSION[$key] = null;
     unset($_SESSION[$key];
}

if (isset($_COOKIE[session_name()])) {
    setcookie(session_name(), '', time()-42000, '/');
}

session_destroy();

//header("Location: goodbye.php");
echo("<script type='text/javascript'>parent.location='goodbye.php'</script>");

//exit();
?>

 

Try that out and see where it gets you.

 

That would of given a parse error, here is a fix

 

 

<?php
   session_start();
   include("dbconnect.php");	

// track logout time in statistics if user logged in
if($_SESSION['online'] && $_SESSION['LoginStatus']){	

@mysql_query("UPDATE stats_ppl_online 
         SET logout_time=now() 
         WHERE session_id='".session_id()."'");
}

// kill session
foreach ($_SESSION as $key => $val) {
     $_SESSION[$key] = null;
     unset($_SESSION[$key]); // here would of been the parse error.
}

if (isset($_COOKIE[session_name()])) {
    setcookie(session_name(), '', time()-42000, '/');
}

session_destroy();

//header("Location: goodbye.php");
echo("<script type='text/javascript'>parent.location='goodbye.php'</script>");

//exit();
?>

 

 

But what are you doing on goodbye.php to display that output?

Link to comment
Share on other sites

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.