gyaani_guy
gyaani_guy

Reputation: 3209

preserving a session variable after session_destroy()

from my logout.php :

<?php require_once("includes/session.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php
  if ( isset( $_SESSION['colony_id']))
    $cookie = $_SESSION['colony_id'] ;
  $_SESSION = array();
  if(isset($_COOKIE[session_name()])) {
    setcookie(session_name(), '', time()-42000, '/');
  }
  session_destroy();
    //this fails-  session_start() ;   
    if ( !empty($cookie))
      $_SESSION['colony_id'] = $cookie ;    
    // redirect_to("login.php?logout=1");
?>

I want to end the current session and then start a new session, with one of the variables from the old session in the new session. I tried adding a second session_start statement, but that had no effect. What else can I do ?

Thanks

Edit: I decided to redirect to a new page, on which a fresh session_start() statement created a new session

Upvotes: 1

Views: 2338

Answers (3)

CarpeNoctumDC
CarpeNoctumDC

Reputation: 1760

If you jsut have one variable you want to save, and you absolutly want to destroy the session: Save the variable to a local variable, destroy the session, start a session, and then reload th session variable...

$localvar = $_SESSION['variable'];
session_destroy();
session_start();
$_SESSOIN['variable'] = $localvar;

Upvotes: 1

Amir
Amir

Reputation: 820

See this link :

http://bugs.php.net/bug.php?id=38042

it's a bug in php and it has a patch.

Upvotes: 2

m4tt1mus
m4tt1mus

Reputation: 1641

You could put the session variable into a normal variable, and then destroy the session and put it back in the session after you create a new one.

Upvotes: 1

Related Questions