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.
bool session_destroy( void )
session_unset() function: It deletes only the variables from session and session still exists. Only data is truncated.
bool session_unset( void )
Example 1: This example saving the session by using session.php file.
Before using session_unset() function: Before using the session function it displaying the name and email.
After using session_unset() function: This function destroys the variables like ‘name’ and ’email’ which are using.
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.
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.
- What is Hactoberfest and How One Can Take Participate in it?
- Difference Between Laravel and Ruby on Rails
- Create a Tic-Tac-Toe Game using jQuery
- How to Upload Image into Database and Display it using PHP ?
- CSS | Combine background image with gradient overlay
- How to define jQuery function ?
- How to Design Half-Page Carousel in Bootstrap ?
- What is the meaning of let-* in Angular 2 Templates ?
- Difference between multiple arguments and options object
- Elasticsearch | Differences between Queries and Filters
- How to Check Twitter Bootstrap is Loaded or not ?
- How to iterate through all selected elements into an array ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.