Page 1 of 1

Destroying a session...

Posted: Tue Jun 10, 2003 2:21 pm
by Swede78
I thought this would work, but it doesn't. Hence... I am here, asking for help from helpful people.

I want to destroy a session, but keep 2 session variables. Basically, clean out 20+ session variables, and start over with the same user. Problem is that the user info is stored in sessions. I'd like to avoid unsetting that many variables. I know that's not too much work, but I'd like to understand how to do things better.

Code: Select all

$UserName = $_SESSION['UserName'];
$UserID = $_SESSION['UserID'];

session_unset();
session_destroy();

$_SESSION['UserName'] = $UserName;
$_SESSION['UserID'] = $UserID;
Is what I'm trying to do, possible? Other than just unsetting all the variables I don't want to keep?

Thanks in advance.

Posted: Tue Jun 10, 2003 2:39 pm
by liljester
that way, you terminate one session, and start a completely new session with the vars you want =)

Code: Select all

<?php
$UserName = $_SESSION['UserName']; 
$UserID = $_SESSION['UserID']; 

session_destroy(); 
session_start();

$_SESSION['UserName'] = $UserName; 
$_SESSION['UserID'] = $UserID;
?>

Posted: Tue Jun 10, 2003 4:08 pm
by delorian
It's better to do it this way:

Code: Select all

<?php 
$UserName = $_SESSION['UserName']; 
$UserID = $_SESSION['UserID']; 

$_SESSION = array();
unset($_COOKIE[session_name()]);
session_destroy(); 
session_start(); 

$_SESSION['UserName'] = $UserName; 
$_SESSION['UserID'] = $UserID; 
?>
Session_destroy() doesn't always do what it should do. So for sure I clean the $_SESSION table and remove the session cookie.

Posted: Wed Jun 11, 2003 9:10 am
by Swede78
Thanks,

I see that I forgot that I need to re-start the sessions.

Isn't session_unset() the same as unset($_COOKIE[session_name()]) ?

Posted: Wed Jun 11, 2003 12:34 pm
by delorian
Swede78 wrote:Isn't session_unset() the same as unset($_COOKIE[session_name()]) ?
No, the unset() functions can "kill" all types of variables, session_unset() free all SESSION variables, and nothing else.