iT邦幫忙

0

PHP session_destroy() v.s session_unset()

php
  • 分享至 

  • xImage

我有一個問題,先奉上代碼:

session_start();
$_SESSION['test'] = '001';
session_destroy();
echo $_SESSION['test'];//001

今天才知道在session_destroy執行完後馬上echo $_SESSION還沒被摧毀!!!

我知道官方也有提示說
session_destroy() destroys all of the data associated with the current session. It does not unset any of the global variables associated with the session, or unset the session cookie.

同樣的狀況使用unset

session_start();
$_SESSION['test'] = '001';
//session_destroy();
session_unset();
echo $_SESSION['test'];//找不到

就會發現他確實被清空了。
感覺session_unset足以應付大部分場合,
那這樣會是在甚麼樣的情況下我一定要選擇使用session_destroy而不是session_unset?

mayyola iT邦研究生 2 級 ‧ 2020-08-06 17:55:52 檢舉
session_destroy 可以將session id銷毀,若session id 不存在,session型態變數存在但資料無法傳遞到其他頁面。
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答