Home>Article>Backend Development> What is the difference between session_unset() and session_destroy() in PHP

What is the difference between session_unset() and session_destroy() in PHP

青灯夜游
青灯夜游 Original
2019-03-15 10:34:41 4861browse

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]

What is the difference between session_unset() and session_destroy() in PHP

##session_unset() function

session_unset()The function only deletes the variables in the session, the session still exists; it only truncates the data.

Basic syntax:

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

:php session(including pictures, texts, videos, cases)

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' ; ?>

Output:

What is the difference between session_unset() and session_destroy() in PHP

Example 1: Using the session_unset() function

Before using the session_unset() function, the name and website will be displayed first.

' . '会话还有效'.'
'; } else { echo '
' . '会话已销毁'; } echo $_SESSION['name'].'
'; echo $_SESSION['website'].'
'; ?>

Output:

What is the difference between session_unset() and session_destroy() in PHPAfter 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:

What is the difference between session_unset() and session_destroy() in PHP

Example 2: Using session_destroy() function

session_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:

What is the difference between session_unset() and session_destroy() in PHPDescription: 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'.

What is the difference between session_unset() and session_destroy() in PHPNote: 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn