How can all sessions be destroyed in PHP, except for one specific session?
To destroy all sessions except for one specific session in PHP, you can iterate through all active sessions, check if the session ID matches the specific session ID you want to keep, and destroy all other sessions. This can be achieved by using session_start() to start the session, session_id() to get the current session ID, and session_destroy() to destroy the unwanted sessions.
session_start();
$currentSessionID = session_id();
foreach ($_SESSION as $key => $value) {
if ($key !== $currentSessionID) {
unset($_SESSION[$key]);
}
}
Related Questions
- What are some efficient ways to handle dynamic unit conversions in PHP without resorting to nested loops or excessive if/else statements?
- What best practices should PHP developers follow when handling product IDs and quantities in IPN scripts?
- What are the consequences of not validating form input data in PHP before submitting it to the database?