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:414857browse

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

<?php 
header("content-type:text/html;charset=utf-8");
// 启动会话
session_start(); 
  
//显示会话ID
echo session_id(); 
  
// 检查会话名称是否存在
if( isset($_SESSION[&#39;name&#39;]) ) { 
    echo &#39;<br>&#39; . &#39;会话还有效.&#39;; 
} 
else { 
    echo &#39;<br>&#39; . &#39;会话已销毁&#39;; 
} 
  
$_SESSION[&#39;name&#39;] = &#39;PHP中文网!&#39;; 
$_SESSION[&#39;website&#39;] = &#39;www.php.cn&#39; ; 
  
?>

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.

<?php 
header("content-type:text/html;charset=utf-8");
// 启动会话
session_start(); 
  
// 检查会话名称是否存在
if( isset($_SESSION[&#39;name&#39;]) ) { 
    echo &#39;<br>&#39; . &#39;会话还有效&#39;.&#39;<br>&#39;; 
} 
else { 
    echo &#39;<br>&#39; . &#39;会话已销毁&#39;; 
} 
echo $_SESSION[&#39;name&#39;].&#39;<br>&#39;; 
echo $_SESSION[&#39;website&#39;].&#39;<br>&#39;; 
  
?>

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.

<?php 
header("content-type:text/html;charset=utf-8");
// 启动会话
session_start(); 
  
// 检查会话名称是否存在
if( isset($_SESSION[&#39;name&#39;]) ) { 
    echo &#39;<br>&#39; . &#39;会话还有效&#39;.&#39;<br>&#39;; 
} 
else { 
    echo &#39;<br>&#39; . &#39;会话已销毁&#39;; 
} 
echo $_SESSION[&#39;name&#39;].&#39;<br>&#39;; 
echo $_SESSION[&#39;website&#39;].&#39;<br>&#39;; 
// 使用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.

<?php 
header("content-type:text/html;charset=utf-8");
// 启动会话
session_start(); 
  
// 检查会话名称是否存在
if( isset($_SESSION[&#39;name&#39;]) ) { 
    echo &#39;<br>&#39; . &#39;会话还有效&#39;.&#39;<br>&#39;; 
} 
else { 
    echo &#39;<br>&#39; . &#39;会话已销毁&#39;; 
} 
  
echo $_SESSION[&#39;name&#39;].&#39;<br>&#39;; 
echo $_SESSION[&#39;website&#39;].&#39;<br>&#39;; 
  
$_SESSION = array(); 
  
// 如果想要终止会话,需要删除会话cookie。
// 注意:这将破坏会话,而不仅仅是会话数据!
if (ini_get("session.use_cookies")) { 
    $params = session_get_cookie_params(); 
    setcookie(session_name(), &#39;&#39;, time() - 42000, 
        $params["path"], $params["domain"], 
        $params["secure"], $params["httponly"] 
    ); 
} 
  
// 最后,销毁会话。
session_destroy(); 
  
?>

Output:

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

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