There are two very similar functions session_unset() and session_destroy() in PHP. Both of them are used to delete all variables registered to the session, so what are the differences between them? The following article will introduce to you the difference between session_unset() and session_destroy(). I hope it will be helpful to you. [Video tutorial recommendation:PHP tutorial]
##session_unset() function
session_unset()The function only deletes the variables in the session, the session still exists; it only truncates the data.
session_unset( void )
session_destroy() function
##session_destroy()The function will be destroyed All data associated with the current session; however, it does not unset any global variables associated with the session, nor does it unset the session cookie.Basic syntax:
session_destroy( void )
Related topic recommendations
The difference between session_unset() and session_destroy()Let’s take a look at the difference between session_unset() and session_destroy() through code examples
First save the session using the session.php file
' . '会话还有效.'; } else { echo '
' . '会话已销毁'; } $_SESSION['name'] = 'PHP中文网!'; $_SESSION['website'] = 'm.sbmmt.com' ; ?>
Before using the session_unset() function, the name and website will be displayed first.
' . '会话还有效'.'
'; } else { echo '
' . '会话已销毁'; } echo $_SESSION['name'].'
'; echo $_SESSION['website'].'
'; ?>
Output:
After using the session_unset() function, it destroyed the variables like 'name' and 'website' that were being used.
' . '会话还有效'.'
'; } else { echo '
' . '会话已销毁'; } echo $_SESSION['name'].'
'; echo $_SESSION['website'].'
'; // 使用session_unset()函数 session_unset(); ?>
Output:
Example 2: Using session_destroy() functionsession_destroy() function will destroy entire session instead of destroying variables. When session_start() is called, PHP sets a session cookie in the browser. We also need to delete the cookies to completely destroy the session.
' . '会话还有效'.'
'; } else { echo '
' . '会话已销毁'; } echo $_SESSION['name'].'
'; echo $_SESSION['website'].'
'; $_SESSION = array(); // 如果想要终止会话,需要删除会话cookie。 // 注意:这将破坏会话,而不仅仅是会话数据! if (ini_get("session.use_cookies")) { $params = session_get_cookie_params(); setcookie(session_name(), '', time() - 42000, $params["path"], $params["domain"], $params["secure"], $params["httponly"] ); } // 最后,销毁会话。 session_destroy(); ?>
Output:
Description: When executing the echo session_id(); statement, you can see that there is a different session ID, which means that the previous The session has been destroyed, as have all variables and cookies. Because all variables are destroyed, when detecting whether the session exists, it will go to the else conditional output 'Session has been destroyed'.
Note: If you wish to terminate the session, please also delete the session cookie. This will destroy the session, not just the session data.
Related learning recommendations:PHP programming from entry to proficiency
The above is the detailed content of What is the difference between session_unset() and session_destroy() in PHP. For more information, please follow other related articles on the PHP Chinese website!