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]);
    }
}