簡體   English   中英

PHP 5.1之前的session_regenerate_id

[英]session_regenerate_id before PHP 5.1

我一直在研究我正在開發的登錄類中使用session_regenerate_id,並且從閱讀PHP文檔和其他一些站點開始,由於該函數是在PHP 4.3.2中添加。

從PHP 5.1開始,它具有delete_old_session參數,如果設置為true,它也會破壞先前的會話,但在先前的版本中則不會。

我的問題是,如果要在運行PHP版本低於5.1的服務器上使用session_regenerate_id,使用session_regenerate_id並銷毀上一個會話的最佳方法是什么?

我不認為session_destroy()會起作用,因為如果我在session_regenerate_id之前使用它,則它將無法攜帶以前的會話數據,如果在它之后使用,則會破壞新的會話。

這應該可以解決您的問題:

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

現在,您的舊會話數據將被刪除並傳輸到新的會話ID。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM