How can one ensure that a session is destroyed when the browser is closed in PHP?

To ensure that a session is destroyed when the browser is closed in PHP, you can set the session cookie to be a session cookie by specifying a lifetime of 0. This way, the session cookie will expire when the browser is closed, effectively destroying the session.

// Start the session
session_start();

// Set the session cookie to be a session cookie
session_set_cookie_params(0);

// Destroy the session
session_destroy();