Change language

session_unset () vs. session_destroy () in PHP

session_destroy():destroys all data associated with the current session. It does not reset session-specific globals, nor does it reset the session cookie.Syntax:
bool session_destroy (void)
session_unset() function:only removes variables from the session, and the session still exists. Only data is truncated.Syntax:
bool session_unset (void)
Example 1:This example of saving a session using the file session.php.  
// Function for starting a session
session_start() ;  
// Show session id echo session_id();  
// Check if the session exists or not if (isset ( $_ SESSION [ ’name’ ])) { echo
. ’session is set.’ ; } else { echo
. ’session is destroyed’ ; } $_ SESSION [ ’name’ ] = ’GeeksForGeeks’ ; $_ SESSION [ ’email’ ] = ’GeeksForGeeks @ email.com’ ;  
?>
Output: Before using session_unset():before using session it displays the name and email address.  
// Function for starting a session
session_start();  
// Check if the session exists or not if (isset ( $_ SESSION [ ’name’ ])) { echo ’session is set.’ ; } else { echo ’ please set the session’ ; } echo $_ SESSION [ ’ name’ ].
; echo $_ SESSION [ ’email’ ].
;  
?>
Output: After using the session_unset() function:this function destroys used variables of the type " name "and" email ".  
// Function for starting a session
session_start();  
// Check if the session exists or not if (isset ( $_ SESSION [ ’name’ ])) { echo ’session is set.’ ; } else { echo ’ session variables deleted’ ; } echo $_ SESSION [ ’ name’ ]; echo $_ SESSION [ ’email’ ];  
// Using the session_unset() function session_unset();  
?>
Output: The session_destroy() function:destroys the entire session, not the variables. When session_start() is called, PHP sets a browser session cookie. We must delete cookies to completely destroy the session.Example:this example is used to destroy the session.  
// Function for starting a session
session_start();  
// Check if the session exists or not if (isset ( $_ SESSION [ ’name’ ])) { echo ’session is set.’ .
; } else { echo ’ session is destroyed’ .
; } echo $_ SESSION [ ’ name’ ].
; echo $_ SESSION [ ’email’ ].
; $_ SESSION = array ();  
// If you want to kill the session, also
// delete the session cookie.
// Note: this will destroy the session, and
// not just 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();  
?>
Output:
When executing the file session.php you can see that there is a different session ID, this means that the previous session was destroyed and all variables and cookies were also destroyed. Since all the variables are destroyed, so PHP goes to another condition, it prints "session destroyed". 
Note.If you need to end the session, also delete the session cookie ... This will destroy the session, not just the session data.

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method