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

青灯夜游
Release: 2023-04-05 15:52:02
Original
4845 people have browsed it

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 )
Copy after login

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 )
Copy after login

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' ; ?>
Copy after login

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'].'
'; ?>
Copy after login

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(); ?>
Copy after login

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(); ?>
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!