4
votes

In my application, when the user logs out, I want to destroy all the current user's sessions. Do I unset each session used in the application and then call session_destroy() or just call session_destroy()?

Thank you!

3
Oh, by "sessions", do you mean indices of the $_SESSION variable? Or actually user sessions? - Jonah
All current user's sessions set within the application. - anon445699
@Scott W: Then session_destroy() itself won't cut it. My I ask why you want to log everyone out when only one user logs out? - Jonah
Jonah, I think you misunderstood my question. As it is stated in my question, I only want destroy the current user's sessions upon logging out. - anon445699
OK I see, I made a typo. I said users instead of user's. My bad... - anon445699

3 Answers

1
votes

session_destroy() does not destroy all user's sessions. You would need to write to a persistent storage media (database, text file, etc.) and then call session_destroy() to kill it's own session. Then, have all pages check it when they load. If it has some special command in it (for example, normal is 0, destroy command is 1), have them call session_destroy().

0
votes

session_unset(): Remove all session vars. In the 1rst F5 no longer display the session variables.

session_destroy(): Delete the current session. In the 2dn F5 no longer display the session variables.

Therefore your logout.php script could be:

<?php
session_start();

...

// remove all session variables
session_unset(); 

// destroy the session 
session_destroy();

// Redirect to home
header("Location: home.php");
exit();
-1
votes

The session_destroy() function should unset all sessions that you have set. So yes, you should only have to call that. You can test it by calling session_destroy() then trying to echo a session value, if it echoes then it's not worked, if an error of some description appears, then the session has successfully been destroyed.