简体   繁体   English

PHP 5.1之前的session_regenerate_id

[英]session_regenerate_id before PHP 5.1

I've been looking at using session_regenerate_id in a login class which I have been developing and from reading the PHP documentation and a few other sites it seems that it creates a new session with a newly generated ID carrying across the previous data since the function was added in PHP 4.3.2. 我一直在研究我正在开发的登录类中使用session_regenerate_id,并且从阅读PHP文档和其他一些站点开始,由于该函数是在PHP 4.3.2中添加。

Since PHP 5.1 it has a delete_old_session parameter and if set to true it will also destroy the previous session but in previous versions it will not. 从PHP 5.1开始,它具有delete_old_session参数,如果设置为true,它也会破坏先前的会话,但在先前的版本中则不会。

My question is if I was to use session_regenerate_id on a server running a PHP version below 5.1 what would be the best way to use session_regenerate_id and to destroy the previous session? 我的问题是,如果要在运行PHP版本低于5.1的服务器上使用session_regenerate_id,使用session_regenerate_id并销毁上一个会话的最佳方法是什么?

I don't think session_destroy() would work because if I used it before session_regenerate_id then it wouldn't be able to carry across the previous session data and if used after it would just destroy the new session. 我不认为session_destroy()会起作用,因为如果我在session_regenerate_id之前使用它,则它将无法携带以前的会话数据,如果在它之后使用,则会破坏新的会话。

This should solve your problem: 这应该可以解决您的问题:

session_start();
// gets current (previous) session
$previousID = session_id();
session_regenerate_id();
// get the new session id
$newID = session_id();
// close session related files
session_write_close();

// set the old session id
session_id($previousID);
// start the old session
session_start();
// clear the old session
session_destroy();
// save the old session state (destroyed session)
session_write_close();

// set the regenerated session id
session_id($newID);
// start the new session
session_start();

Now your old session data is erased and transfered to a new session id. 现在,您的旧会话数据将被删除并传输到新的会话ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM