How can PHP sessions be properly ended when the browser is closed?

When the browser is closed, PHP sessions should be properly ended to ensure security and clean up resources. This can be achieved by setting the session cookie to expire when the browser is closed. By doing this, the session data will be deleted when the browser is closed, effectively ending the session.

// Set session cookie to expire when browser is closed
ini_set('session.cookie_lifetime', 0);
session_start();