session_regenerate_id(true) not work [duplicate]

2020-07-20 15:46发布

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

标签: php
3条回答
神经病院院长
2楼-- · 2020-07-20 16:21

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();
查看更多
成全新的幸福
3楼-- · 2020-07-20 16:23

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();

查看更多
唯我独甜
4楼-- · 2020-07-20 16:28

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

查看更多
登录 后发表回答