session_unset() vs session_destroy() in PHP

There are two very similar PHP function session_destroy() & session_unset(). Both seem to delete all variables registered to a session but there is difference between them.

session_destroy() function: It 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.

Syntax:

bool session_destroy( void )

session_unset() function: It deletes only the variables from session and session still exists. Only data is truncated.

Syntax:

bool session_unset( void )

Example 1: This example saving the session by using session.php file.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Function to start session
session_start();
  
// Display the session id
echo session_id();
  
// Check the session name exists or not
if( isset($_SESSION['name']) ) {
    echo '<br>' . 'session is set.';
}
else {
    echo '<br>' . 'session is destroyed';
}
  
$_SESSION['name'] = 'GeeksForGeeks';
$_SESSION['email'] = 'GeeksForGeeks@email.com' ;
  
?>

chevron_right


Output:

Before using session_unset() function: Before using the session function it displaying the name and email.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Function to start session
session_start();
   
// Check the session name exists or not
if( isset($_SESSION['name']) ) {
    echo 'session is set.';
}
else {
    echo 'please set the session';
}
   
echo $_SESSION['name'].'<br>';
echo $_SESSION['email'].'<br>';
  
?>

chevron_right


Output:

After using session_unset() function: This function destroys the variables like ‘name’ and ’email’ which are using.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Function to start session
session_start();
   
// Check the session name exists or not
if( isset($_SESSION['name']) ) {
    echo 'session is set.' ;
}
else {
    echo 'session variables deleted';
}
  
echo $_SESSION['name'];
echo $_SESSION['email'];
  
// Use session_unset() function
session_unset();
  
?>

chevron_right


Output:

session_destroy() function: It destroys the whole session rather destroying the variables. When session_start() is called, PHP sets the session cookie in browser. We need to delete the cookies also to completely destroy the session.

Example: This example is used to destroying the session.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Function to start session
session_start();
  
// Check the session name exists or not
if( isset($_SESSION['name']) ) {
    echo 'session is set.'.'<br>' ;
}
else {
    echo 'session is destroyed'.'<br>';
}
  
echo $_SESSION['name'].'<br>';
echo $_SESSION['email'].'<br>';
  
$_SESSION = array();
  
// If it's desired to kill the session, also
// delete the session cookie.
// Note: This will destroy the session, and
// not just the session data!
if (ini_get("session.use_cookies")) {
    $params = session_get_cookie_params();
    setcookie(session_name(), '', time() - 42000,
        $params["path"], $params["domain"],
        $params["secure"], $params["httponly"]
    );
}
  
// Finally, destroy the session.
session_destroy();
  
?>

chevron_right


Output:

The execution of session.php file you can see that there is a different session ID it means the previous session has been destroyed and all variables and cookies also destroyed. Since all variables destroyed so PHP go to else condition output ‘session is destroyed’.

Note: If it’s desired to kill the session, also delete the session cookie. This will destroy the session, and not just the session data.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.