3

Possible Duplicate:
Close session and start a new one

I use wampserver2.1 php5.3.3, I found session_regenerate_id(true) not work in my script,document says when I set the parameter 'delete_old_sessions' true, there should be a new sid and all the session variables should be deleted, but the fact is after the function, $_session[abc] is still there. did I misunderstand the function,what is my problem? I appreciate if anyone can help me,

<?php
session_start();
$_SESSION['abc']=12323;
session_regenerate_id(true);
echo $_SESSION['abc'];                  
?>

I thought it should display none, but it outputs:12323

Community
  • 1
  • 1
user995789
  • 279
  • 1
  • 4
  • 15

3 Answers3

5

session_regenerate_id() updates the current session id with a newly generated one. It does not change session variables.

echo session_id();
session_regenerate_id();
echo session_id(); 

You should unset session to do that:

unset($_SESSION); // or
$_SESSION = array();

How to start a new session:

session_start();
session_destroy();
session_regenerate_id();
unset($_SESSION);
session_start();
Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
1

session_regenerate_id sends a new cookie but doesn't overwrite the value stored in $_COOKIE. After calling session_destroy, the open session ID is discarded, so simply restarting the session with session_start will re-open the original, though now empty, session for the current request (subsequent requests will use the new session ID). Instead of session_destroy+session_start, use the $delete_old_session parameter to session_regenerate_id to delete the previous session data.

<?php
session_start();
/* Create a new session, deleting the previous session data. */
session_regenerate_id(TRUE);
/* erase data carried over from previous session */
$_SESSION=array();
?>

To start a new session and leave the old untouched, simply leave out the argument to session_regenerate_id.

Source: http://de.php.net/manual/en/function.session-regenerate-id.php#107323

stdclass
  • 2,932
  • 2
  • 19
  • 16
0

If you want to destroy the session-variables you can perform this: session_destroy(); and if you want to get new ID you can session_regenerate_id();

Rob
  • 3,556
  • 2
  • 34
  • 53